struts2中action之间使用chain传值的探索

struts2中action之间使用chain传值的探索


一.相关代码
1.连续使用三个action的struts.xml(仅action部分)。
<action name="action" class="tutorial.Action">
            <result type="chain">
             action1
            </result>
        </action>
        <action name="action1" class="tutorial.Action1">
            <result type="chain">
             action2
            </result>
        </action>
        <action name="action2" class="tutorial.Action2">
            <result>
             failure.jsp
            </result>
        </action>


2.三个action类


Action.java
package tutorial;
import com.opensymphony.xwork2.ActionSupport;
public class Action extends ActionSupport{
 private static final long serialVersionUID = 1L;
 String msg = "";
 public String getMsg() {
 return msg;
 }
 public void setMsg(String msg) {
  this.msg = msg+"0!";
  System.out.println("Action0.setter;");
  System.out.println(msg);
 }
 public String execute(){
 return SUCCESS;
 }
}

 

Action1.java
package tutorial;
import com.opensymphony.xwork2.ActionSupport;
public class Action1 extends ActionSupport{
 private static final long serialVersionUID = 1L;
 String msg = "";
 public String getMsg() {
 return msg;
 }
 public void setMsg(String msg) {
  this.msg = msg+"1!";
  System.out.println("Action1.setter;");
  System.out.println(msg);
 }
 public String execute(){
 return SUCCESS;
 }
}


Action2.java
package tutorial;
import com.opensymphony.xwork2.ActionSupport;
public class Action2 extends ActionSupport{
 private static final long serialVersionUID = 1L;
 String msg = "";
 public String getMsg() {
 return msg;
 }
 public void setMsg(String msg) {
  this.msg = msg+"2!";
  System.out.println("Action2.setter;");
  System.out.println(msg);
 }
 public String execute(){
 return SUCCESS;
 }
}

3.提交参数页面
SayHello.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>Say Hello</title>
    </head>
    <body>
            <s:form action="action">
            Msg: <s:textfield name="msg" />
            <s:submit />
        </s:form>
    </body>
</html>
4.最后一个action转向页面failure.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>success</title>
</head>
<body>
<h1>你输入的是<font color="red"><s:property value="msg"/></font>不是yeepay</h1>
</body>
</html>

 

二说明
       通过SayHello.jsp向name为action的action传递值(a),最后在failure.jsp页面中接收到的参数是只经过action2处理的结果(即a2!),而这个结果没有action和action1处理的痕迹。此外,在服务器打印的效果如下:
Action0.setter;
a
Action1.setter;
a0!
Action1.setter;
a
Action2.setter;
a0!
Action2.setter;
a1!
Action2.setter;
a


      从这个结果可以看出,首先是action(即第一个action)接受了表单参数,并在服务器打印了参数;然后action1(即第二个action)先接收action(即第一个action)传递过来的参数,然后接收表单参数;类似的,action2(即第3个action)先接收action(即第一个action)的参数,再接收action1(即第二个action),最后接收表单传递过来的参数。从这里可以推测,之所以使用<result type="chain">传值时最后的页面不能接收到中间action的值是因为,对每个action而言,表单始终会作为终结者传递一次参数,所以最后在页面显示的只是最后一个action(例子中是action2)处理表单参数后的结果。
这里我们也就可以基本得出一个结论,<result type="chain">的使用主要是为了共享表单传递的数据,即实现一个表单参数传递多个action的目的。

 

   三.最后,要使用<result type="chain">传递action处理后的参数可以通过如下方法实现。代码如下:


Struts.xml的action部分
<action name="action" class="tutorial.Action">
            <result type="chain">
             action1
            </result>
        </action>
        <action name="action1" class="tutorial.Action1">
            <result>
             failure.jsp
            </result>
        </action>


Action.java
package tutorial;
import com.opensymphony.xwork2.ActionSupport;
public class Action extends ActionSupport{
 private static final long serialVersionUID = 1L;
 String msg = "";
 String bai ="";
 public String getMsg() {
  return msg;
 }
 public void setMsg(String msg) {
  this.msg = msg+"0!"; 
  this.bai=this.msg;
 }
 public String execute(){
  return SUCCESS;
 }
 public String getBai() {
  return bai;
 }
}


Action1.java
package tutorial;
import com.opensymphony.xwork2.ActionSupport;
public class Action1 extends ActionSupport{
 private static final long serialVersionUID = 1L;
 String bai="";
 public String execute(){
  return SUCCESS;
 }
 public String getBai() {
  return bai;
 }
 public void setBai(String bai) {
  this.bai = bai+"a2!";
  System.out.println(bai);
 }
}


failure.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>success</title>
</head>
<body>
<h1>你输入的是<font color="red"><s:property value="bai"/></font>不是yeepay</h1>
</body>
</html>

 

     这时,通过SayHello.jsp(输入a)提交参数到action(第一个action)处理后的结果可以再被action1(第二个action)处理后再显示到failure.jsp中(a0!a2!),达到了我们想要的效果。


     不过网上对这个问题最主流的解决办法还是使用<result type="redirect">,将值传递到下一个action同时以url的形式传递参数。


     希望这篇文章能抛砖引玉,有牛人能够修改chain类型对应的源码,进一步方便我们开发


转载地址:http://hi.baidu.com/cl770511/item/48945397594f9e3b336eeb95

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值