struts中在两个action之间进行传递数据的方法

首先有一个假设,这两个action中的form都是同一个类型,第一个action称为action1,第二个action称为action2
在strutts中,若从action1跳转到action2的话,则在action2中,struts会重新从reqeust(或session或context)中取出数据,重新设置到form中.所以,若想在两个action之间传递数据,不可以在action1中设置form,而是通过request,可以直接覆盖想传递的属性parameter或者attribute, 在第二个action中,struts会自动取出数据,设置到form中,也可以使用别的名称,在action2手动取出这个数据,在手动设置到form中.
那为什么呢?其实这不是struts的设计失误,而是实际情况没办法做到用form来传递数据,因为action2不清楚这是从别的action传递过来的,有可能是从client端传过来的.还有,action2也不知道这个form是从什么地方来的.所以action2要重新设置form的值.

【songsongsong84】:
两种方法:

一把参数放在 request.setAttribuite里
在下个action 里头用request.get  或者的该值

还有个就 把参数放 session 的属性里头


如果不知道具体怎么操作就说一声,我帮你写,不过要记得,给分我需要分去问别人问题

【qulin52】:
这个方法是得不到值的,我早就试过了
request.setAttribute("sysType", sysTypeId);
get 到的是null

【qulin52】:
request.getSession().setAttribute("sysType",sysTypeId);
放session 中也是得不到

【qulin52】:
我以前在网上看到过 有个解决方法 只是比较麻烦的!


【songsongsong84】:
那不可能,,action 都是servlet  难道在两个servlet中都不能用session 传值

【qulin52】:
问题是等不到 ~ 我直接给我数值都得不到

【songsongsong84】:
怎么写的?

【qulin52】:
request.getSession().setAttribute("sysType",sysTypeId);
request.getSession().setAttribute("netType",netTypeId);


sysTypeId=(String) request.getAttribute("sysType");
netTypeId=(String) request.getAttribute("netType");

【qulin52】:
我确定systypeId在传的时候不为空!

【songsongsong84】:
sysTypeId=(String) request.getAttribute("sysType");
netTypeId=(String) request.getAttribute("netType");

改为:


你要记得 HttpSession session=request.getSession();
sysTypeId=(String) session.getAttribute("sysType");
netTypeId=(String) session.getAttribute("netType");

【songsongsong84】:
你根本没写对啊,你变量放在session 的属性里头就的用session 来获取啊,不要用request 啊

【qulin52】:
我试试

【mianwo602】:
<action path="/tawwpyear/checklistpass" parameter="CHECKLISTPASS"
type="com.boco.eoms.workplan.controller.TawwpYearAction"
scope="request" validate="false">
<forward name="success" path="/tawwpyear/checklist.do"
redirect="true" />
</action>
你这里写了 redirect="true",这个是直接跳转,也就是说,你在第一个action中set的值,在第2个action,中是获取不到的。用session可以,我觉得不要怎么做
解决办法:去掉redirect="true"这个,就可以了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值