new ActionForward和mapping.findForward(请求重定向和转发)
1.struts 中return new ActionForward(URI,true) 与 <forward name = "/success" path = "/page/success.jsp"/>。
1. 在ACTION里 使用
String URI = "";
return new ActionForward(URI,true);的组合,一般是因为需要在request中加入 parameter ,但如果此时在这个action里试图加入 Attribute,则转向的页面将接收不到 Attribute的值。
2. 使用return new ActionForward(mapping.findForward("/success"));
表示已经在struts的action-config.xml中配置了success的转向,如<forward name = "/success" path = "/page/success.jsp"/>。
补充:
ActionForward类提供了下面五种构造器:
public ActionForward() public ActionForward(String path) 常用 public ActionForward(String path, boolean redirect) public ActionForward(String name, String path, boolean redirect) public ActionForward(String name, String path, boolean redirect, boolean contextRelative)
虽然这些构造器是不需要说明的,但我们应该注意下面几点。在这些构造器中,第二种可能是最常用的。
后四种构造器中的path参数表示的是到目的资源的路径。
后三种构造器中的redirect布尔值表示的是是否执行了一个重定向(redirect)。(缺省情况下,这个值设置为false,因为redirect比forward慢。)
最后,第五个构造器中的contextRelative布尔值表示该路径是否应该是context-relative的,而不是module-relative的。
如
return (new ActionForward("/mainMenu.jsp")); return (new ActionForward("/login.jsp"));