Struts 2.x 学习----------------跳转配置

关于Struts 2.x的学习记录:

六、跳转配置
在Struts 2.x 里面所有的跳转都是由Action进行的,而且只要想进行跳转,只需要在相应的方法里面返回指定的字符串即可,而这些字符串都会在struts.xml文件里面出现相应的“<result>”节点
范例:观察已有代码
public String execute() throws Exception{
        this.msg="ECHO:"+this.msg;
        return "echo.page";//返回路径的映射的key
}
<result name="echo.page">echo.jsp</result>
在“<result>”节点里面保存的是要跳转的路径,但是对于这个路径,一定要记住它是与包匹配的
**包的定义:
<package name="root" namespace="/" extends="struts-default">
|-namespace表示的是整个可以访问程序的公共路径,有了这个路径之后,在此包中配置的一切页面,如果没有明确声明,都是在此路径下的
**result的定义:<result name="echo.page">echo.jsp</result>
|-组合:namespace/result,所以最终这个跳转路径是:“/echo.jsp”
很多时候写这种相对路径可能有很多人并不习惯,所以可以使用绝对路径:<result name="echo.page">/pages/demo/echo.jsp</result>
但是对于跳转结果的返回内容,实际上在ActionSupport类里面也有几个常量支持,这几个常量都有其特殊的使用环境,首先来观察常量(都在com.opensymphony.xwork2.Action接口里定义)
**成功操作:public static final String SUCCESS,"success";
**错误操作:public static final String ERROR,"error";
**重新登录:public static final String LOGIN,"login";
**服务器验证未通过:public static final String INPUT,"input";
**不做任何操作:public static final String NONE,"none";
通过代码结构可以发现ActionSupport是Action接口的子类
如果在开发过程之中也可以使用这些常量来作为跳转的key
public String execute() throws Exception{
        this.msg="ECHO:"+this.msg;
        return Action.SUCCESS;//返回路径的映射的key
}
<action name="EchoAction" class="com.lyt.action.EchoAction">
            <!-- 定义跳转路径 -->
            <result name="success" type="dispatcher">echo.jsp</result>
</action>
这些常量在自己的开发之中不是必须编写的,只是留给标准使用的

猜你喜欢

转载自blog.csdn.net/amuist_ting/article/details/80849164
今日推荐