struts2结果(Result)

一、结果(result)类型

result的type属性默认为dispatcher,其他常见的属性有redirect\chain\redirectAction

<action name="a1">
             <result type="dispatcher">
                 /a1.jsp
             </result>
         </action>
         <action name="a2">
             <result type="redirect">
                 /a2.jsp
             </result>
         </action>
         <action name="a3">
             <result type="chain">
                 a1
             </result>
         </action>
         <action name="a4">
             <result type="chain">
                 <param name="actionName">a1</param>
                 <param name="namespace">/</param>
             </result>
         </action>
         <action name="a5">
             <result type="redirectAction">
                 a1
             </result>
         </action>

dispatcher就是forward到一个jsp页面

redirect就是跳转到一个jsp页面

chain就是forward到一个action,跳转到另外一个package用第四种方式

redirectAction就是跳转到一个action

 

二、全局结果集

在配置struts2.xml时有时候会遇到一个问题即多个action的result是相同的这时候就没有必要为每个action配一个result,只要配置一个global result即可:

<package name="index" namespace="/" extends="struts-default">
         <global-results>
             <result name="msg">/msg.jsp</result>
         </global-results>
     </package>
     <package name="user" namespace="/user" extends="index">
         
     </package>

这样当index包和user包下的action返回msg的时候就会forward到/msg.jsp。(extends="index"从index包继承)

 

三、动态结果集

在struts.xml中配置:

<action name="login" class="cn.orlion.user.UserAction" method="login">
             <result>
                 ${r}
             </result>
         </action>

UserAction:

package cn.orlion.user;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{
    
    private int type;
    
    private String r;
    
    public String login(){
        
        if (1 == type) r = "/a1.jsp";
        else if (2 == type) r = "/a2.jsp";
        return "success";
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getR() {
        return r;
    }

    public void setR(String r) {
        this.r = r;
    }
    
}

这样当访问http://localhost:8080/Struts2Demo/user/login.action?type=1时就会看到a1.jsp

访问...?type=2时就会看到a2.jsp了

struts.xml中${r} 实际是从Value Stack中取出r的值

 

四、结果集传参数

当访问.../login.jsp?uid=test时通过< s:property value="uid" />是取不到的因为uid是个参数不会放到Value Stack中而是放到Stack Context中,可以通过<s:property value="#parameters.uid" />娶到。

当访问一个action(在这个action里赋值给了uid)时,这个action的结果:login.jsp(即forward到 /login.jsp)可以用<s:property value="uid" />取到值,这是因为forward可以从Value Stack中取到。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值