new ActionForward和mapping.findForward的区别

来源:互联网 发布:看鬼畜视频的软件 编辑:程序博客网 时间:2024/06/10 20:06

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