ActionForward

在struts中实现跳转就在Action中用ActionForward,然后再struts-config.xml实现跳转。

AcrionForward默认实现的是转发,使用的是一个request,其中url地址不变;重定向使用的是两个request,其中url地址改变。可以在forward中设置重定向属性为true那么就把ActionForward变为了重定向。

一,全局ActionForward与重定向

在一个forward中加入redirect则表示一个action中实现了重定向:

<forward name="login" path="/login.jsp" redirect="true"/>

如果想把forward跳转在其他的action中也能使用,那么可以把该forward设置为全局跳转,代码:

<global-forwards>

    <forward name="login" path="/login.jsp" redirect="true"/>

</global-forwards>

假如Action代码中查找的跳转在action配置中不存在,那么就会到全局forward中去查找。

也可以在Action中使用代码设置重定向:

注意如果在代码中不能使用ActionForward 对象的setRedirect()方法来做重定向,因为这样会修改struts-config.xml中的属性,但是stuts-config.xml中的属性在运行期间不能更改,所以这种重定向失败。如果不想让struts来做重定向,那么向ActionForward对象返回一个null,之后使用response的sendRedirect()来重定向。

不能写如下代码:

ActionForward af=mapping.findForward("login");

af.setRedirect(false);

//Action中设置重定向

response.sendRedirect("/login.jsp");

return null;

二,动态ActionForward

假如一个Action中需要跳转很多个页面,可以使用动态ActionForward来实现,然后struts-config.xml中不需要配置forward。

Action中的代码:

//page表示区别不同页面的参数

String page=request.getParameter("page");

ActionForward af=new ActionForward();

af.setPath("/page"+page+".jsp?name=tom");

return af;

原文地址:https://www.cnblogs.com/jinzhengquan/p/1954606.html