首页 > 代码库 > Struts学习笔记_Result

Struts学习笔记_Result

一,Result type

 <package name="resultTypes" namespace="/r" extends="struts-default">        <action name="r1">            <result type="dispatcher">/r1.jsp</result> //服务器跳转,为默认项        </action>                <action name="r2">            <result type="redirect">/r2.jsp</result>  //客户端跳转,跳转后url变成jsp的路径        </action>                <action name="r3">            <result type="chain">r1</result>      //服务器跳转可以跳转到action,        </action>                <action name="r4">            <result type="redirectAction">r2</result> //客户端跳转可以跳转到action        </action>            </package>

 二,global result

 

<struts>    <constant name="struts.devMode" value="true" />    <package name="user" namespace="/user" extends="struts-default">                        <global-results>            <result name="mainpage">/main.jsp</result>  //全局result        </global-results>                <action name="index">            <result>/index.jsp</result>        </action>                <action name="user" class="com.bjsxt.struts2.user.action.UserAction">            <result>/user_success.jsp</result>            <result name="error">/user_error.jsp</result>        </action>            </package>        <package name="admin" namespace="/admin" extends="user"> //extends继承user包,可以调用user里面的内容        <action name="admin" class="com.bjsxt.struts2.user.action.AdminAction">            <result>/admin.jsp</result>        </action>    </package></struts>

 

 useraction

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 {        if(type == 1) return "success";        else if (type == 2) return "error";        else return "mainpage";  // 调用全局result    }}

 

 AdminAction

public class AdminAction extends ActionSupport {    @Override    public String execute() throws Exception {        return "mainpage";    }}

 

 三,动态结果集

<struts>    <constant name="struts.devMode" value="true" />    <package name="user" namespace="/user" extends="struts-default">                <action name="user" class="com.bjsxt.struts2.user.action.UserAction">            <result>${r}</result> //取值栈中r的值        </action>            </package>        </struts>

 

//UserAction .java   一定不要忘了为动态结果的保存值设置set get方法
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";
    }

}

 

四,带参数的result,一次request公用一个值栈,服务器端调用是一次request,客户端两次

<struts>    <constant name="struts.devMode" value="true" />    <package name="user" namespace="/user" extends="struts-default">                <action name="user" class="com.bjsxt.struts2.user.action.UserAction">            <result type="redirect">/user_success.jsp?t=${type}</result>    // 客户端调用 ${type}取得action传入值栈中的type值        </action>            </package>        </struts>

 

UserAction 
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";    }}

 

user_success.jsp
<body>    User Success!    from valuestack: <s:property value="t"/><br/>  //由于是客户端调用,不共用一个值栈,所以值栈中为空,不能再值栈中取值    from actioncontext: <s:property value="#parameters.t"/>//只能在actionContext中取值    <s:debug></s:debug></body>

 

Result总结

  1. 常用四种类型:

    a)         dispatcher(默认)

    b)         redirect

    c)         chain

    d)         redirectAction

  2.全局结果集

    a)         global-results | extends

  3.动态结果(了解)

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

  4.传递参数

    a)         客户端跳转才需要传递

    b)         ${}表达式(不是EL)

 

Struts学习笔记_Result