• java之struts2的配置讲解(2)


    java之struts框架入门教程

    基础上,进行下列操作

    1.结构对比

    原来的项目结构图

    现在的结构图

    即从结构上可以看出,在HelloStruts项目中增加了config 文件夹(Source Folder) 及user.xml 文件

    2.修改配置文件,使struts.xml 中包含 user.xml 配置文件

    struts.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
        
        <!-- 常量配置 -->
        <!-- 解决乱码 -->
        <constant name="struts.i18n.encoding" value="utf-8"/>
        <!-- action扩展名配置 -->
        <constant name="struts.action.extension" value="do,action,,zhangsan"/>
        <!-- 配置开发模式 -->
        <constant name="struts.devMode" value="true"/>
        
        <!-- 加载另外的配置文件  在团队协作中使用 -->
        <include file="cn/qm/struts/user.xml"></include>
    
    
        
    </struts>

    user.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
    
        <!-- package表示包   name是自定义的 一般和模块名称相关  name在整个项目中唯一
    extends 表示继承 必须直接或者间接继承struts-default 因为在struts-default中 定义了struts2的相关功能。
    namespace 表示命名空间 和分模块开发相关 直接决定请求的url匹配工作 ,一个请求的url被action匹配 需要加上namespace 如:namespace="/user",该namespace下有一个add的action,那么要请求 该action的url为/user/add.action namespace也是为分工协作使用
    --> <package name="default" extends="struts-default" namespace="/sys"> <!-- action的配置 一个action表示一个请求
    name表示请求的url名称去掉后缀,在同一个 包下唯一
    class 表示处理请求的类的完全限定名=包名+类名,
    如果不写 默认由com.opensymphony.xwork2.ActionSupport
    method 指明处理请求的方法名称,默认是execute方法
    处理方法 必须是 public String xxxx(){};无参方法
    --> <action name="hello" class="cn.qm.action.HelloAction" method="hello"> <!-- result表示结果集处理 name和action中处理方法的返回值匹配 默认为success struts的Action接口 提供了5个返回值类型    Action.SUCCESS 表示处理方法执行成功    Action.NONE 表示处理方法执行成功 但是不需要视图显示    Action.ERROR 表示处理方法执行失败   Action.INPUT 表示处理方法需要更多的输入信息 才能执行成功    Action.LOGIN 表示处理方法不能执行,需要用户登录 type表示结果集的跳转类型 默认是转发   dispatcher 转发   redirect 重定向   redirectAction 跳转到另外一个Action stream 流 值 /表示根路径 --> <result name="success" type="dispatcher">/index.jsp</result> </action> </package> </struts>

    3.修改 HelloAction 类,增加 hello 方法

    public class HelloAction {
    
        ////struts2的处理方法 都是 public String的  默认执行execute,并且处理方法没有参数
        public String execute(){
            System.out.println("请求被接收了...");
            return "success";
        }
        
        public String hello(){
            System.out.println("hello");
            return Action.SUCCESS;
        }
    }

    因为在配置文件中,指定了hello方法,所以请求会进入hello方法

    4.运行程序,并且在浏览器输入网址验证

    网址:http://localhost:8080/Hello/sys/hello.action

    浏览器显示

    myeclipse中的console显示

    说明请求成功被接收了。

  • 相关阅读:
    安全探讨之用Win32汇编写双进程守护
    LightTPD 1.4.12
    mysql4存在mysql5没有的性能成绩
    gcolor2-拾色器
    solaris 中挂载usb移动硬盘
    Browsershots:测试你的 Web 企图
    MythTV 0.20
    XorgEdit:xorg.conf 编纂器
    pci168c,1c无线网卡如何在64位Solaris系统上运用
    Fedora8中批改磁盘卷标
  • 原文地址:https://www.cnblogs.com/Vincent-yuan/p/10872962.html
Copyright © 2020-2023  润新知