Struts2学习第六天:Result总结

来源:互联网 发布:英语学霸软件 编辑:程序博客网 时间:2024/05/22 14:44

/Struts2_1600_GlobalResult 公用的结果集

1.如果在加载过程中struts.xml文件配置错误,则必须重新加载项目。

<constant name="struts.devMode" value="true"/> 仅用于加载没问题Struts2帮助文档:\struts-2.1.6-all\struts-2.1.6\docs index.html

2.全局结果集:在struts.xml的package中配置:在extends ActionSupport 的类中写个返回String “main”的方法即可

        <global-results>            <result name="main">                /main.jsp            </result>        </global-results>

3.从一个package访问另一个package的 globalresult可通过extends属性

<package name="admin" namespace="/admin" extends="user"><package name="user" namespace="/user" extends="struts-default"> 默认的继承

必须有extends该属性,在referenced library/struts2-core-2.1.6.jar/struts-default.xml

/Struts2_1700_DynamicResult 动态结果集,使用不多,了解即可:

可以属性保存结果(r);属性的结果可以动态确定;在Struts.xml可以${r} 从值栈取值

1. 在Struts.xml中配置result如下:

<result>${r}</result>  不是EL表达式,是OGN表达式  $用于在配置文件中,从值栈中取值

2.在Controller中配置:

type通过index.jsp < li>< a href=”user/user?type=1”>返回success < / a> < / l i >

public class UserAction extends ActionSupport {            private int type;            private String r;            public String getR() {                return r;            }            public void setR(String r) {                this.r = r;            }            public int getType() {                return type;            }            public void setType(int type) {                this.type = type;            }            @Override            public String execute() throws Exception {                if(type == 1) r="/user_success.jsp";                else if (type == 2) r="/user_error.jsp";                return "success";            }        }

/Struts2_1800_ResultWithParams

1.配置struts.xml

<result type="redirect">/user_success.jsp?t=${type}</result>

是客户端跳转,只有在redirect(客户端)时才需要传
一个Action forward 到其他的Action,不需要传参数,forward共享同一个值栈
一次request只有一个值栈 因而采用 dispatcher 不需要传参数,可直接取值

2.在Action中声明type

        public class UserAction extends ActionSupport {            private int type;            public int getType() {                return type;            }            public void setType(int type) {                this.type = type;            }            @Override            public String execute() throws Exception {                return "success";            }        }

3.在jsp中取值

from valuestack: <s:property value="t"/><br/><!- - 第一个取不到,因为新的请求没有Action的存在,t的参数无法放在值栈 - ->from actioncontext: <s:property value="#parameters.t"/><!- - 可以取到, - ->

2.Result总结

1. 常用四种类型:

        a) dispatcher(默认)        b) redirect        c) chain        d) redirectAction

2. 全局结果集

        a) global-results | extends

3. 动态结果(了解)

        a) 在action中保存一个属性,存储具体的结果location

4. 传递参数

        a) 客户端跳转才需要传递        b) ${}表达式(不是EL)