struts控制器中使用new ActionForward和mapping.findForward的区别

本文探讨了Struts框架中两种不同的页面跳转方式:使用newActionForward与mapping.findForward的区别。具体分析了它们如何处理request及session作用域的数据传递,并讨论了不同redirect属性设置的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

struts控制器中使用new ActionForward和mapping.findForward的区别

request.setAttribute("list", new Integer(0));
return new ActionForward("/success.jsp");

request.setAttribute("list", new Integer(0));
return mapping.findForward("fail");

当使用 return new ActionForward("/success.jsp");的时候相当于还是同一个request请求,所以可以携带参数setAttribute过去。
无论 <forward
name="succ"
path="/success.jsp"
redirect="true" />//无论此处的redirect是true还是false。

当使用 return mapping.findForward("fail");的时候如果 redirect="true",相当于还是另外一个request请求,所以不能携带参数setAttribute过去。
要想还是使用同一个request,获取到参数,则把
<forward
name="succ"
path="/success.jsp"
redirect="false" />//此处的redirect改为false。另外,此处不设置的话默认redirect="false"。

无论在任何情况下使用
request.getSession().setAttribute("a", "sssss");都可以传递参数。
在页面处获取 <%=request.getSession().getAttribute("a")%>

另外,使用new ActionForward时候地址栏不会出现jsp页面,当使用session携带参数的时候会显示sessionid在地址栏,和使用mapping.findForward当redirect改为false时候效果一样。

使用mapping.findForward的时候并且redirect是true时候效果,地址栏如 http://localhost:8087/struts/fail.jsp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值