配置<result…/>元素时通常需要指定如下的两个属性:
(1). name:该属性指定所配置的逻辑视图
(2). type:该属性指定结果类型
<struts>
<package name="owen" extends="struts-default">
<action name="resultAction" class="com.owen.action.LoginRegistAction">
<!—dispatcher就是转到指定的页面,它与direct有区别在后续讲解-->
<result
name=”success” result=”dispatcher”>/WEB-INF/content/welcome.jsp</result>
</action>
<action name="*">
<result>/WEB-INF/content/{1}.jsp</result>
</action>
</package>
</struts>
1. redirect和redirectAction结果类型
1.1 redirect结果类型
dispatcher结果类型与redirect结果类型相对,dispathcer结果类型是将请求forward(转发)到指定的JSP资源;而redirect结果类型,则意味着将请求redirect(重定向)到指定的视图资源。dispatcher结果类型与redirect结果类型的差别主要是转发和重定向的差别:重定向会丢失所有的请求参数、请求属性也丢失了Action的处理。
<struts>
<constant name="struts.devMode" value="true"/>
<package name="lee" extends="struts-default">
<action name="login" class="com.owen.action.LoginAction">
<!-- 指定结果的类型为redirect,
这意味着系统该Action将重定向到welcome.jsp页面-->
<result type="redirect">/welcome.jsp</result>
</action>
<action name="*">
<result>/WEB-INF/content/{1}.jsp</result>
</action>
</package>
</struts>
1.2 redirectAction结果类型
这种结果类型与redirect类型非常相似,一样是新生成一个全新的请求。但redirectAction使用ActionMapperFactory提供的ActionMapper来重定向请求。
配置redirectAction结果类型时,可以指定如下的参数:
(1). actionName:该参数指定重定向的Action名
(2). namespace:该参数指定需要重定向的Action所在的命名空间。
<struts>
<constant name="struts.devMode" value="true"/>
<package name="lee" extends="struts-default">
<action name="login" class="com.owen.action.LoginAction">
<result type="redirectAction">
<param name=”actionName”>owen</param>
<param name=”namespace”>/secure</param>
</result>
</action>
<action name="*">
<result>/WEB-INF/content/{1}.jsp</result>
</action>
</package>
<package name=" secure " extends="struts-default" namespace=”/ secure”>
<action name=" owen " class="com.owen.action.Owen">
<result type="redirectAction"> owen .jsp</result>
</action>
</package>
</struts>