转发:
地址自动跳转,参数自动携带
重定向
type=redirect
如果利用重定向,必须后面通过携带的参数的方式来获取,否则获取不到.
修改重定向
1通过参数修改提交地址
2通过参数param[前台的传参]指定前台的传参
转发和重定向
在Struts.xml的配置中,默认是转发,
当然我们可以手动配置为重定向:
重定向
<action name="Login" class="com.xxx.entity.LoginAction">
<result type="redirect">
<param name="location">/success.jsp</param>
<param name="username">${username}</param>
<param name="password">${password}</param>
</result>
</action>
转发
<action name="second" class="com.xxx.entity.LoginAction" >
<result name="success">/success.jsp</result>
</action>
转发方式两种:
- jsp—>action
- action—>action
所有的转发都不需要写参数,只需要跳转路径
默认转发:dispatcher
地址自动跳转,参数自动携带
修改成重定向:
通过"参数(location)"指定提交地址
通过"参数"指定传参
重定向到action(服务器端内部跳转)
转发到action:chain
通过参数(actionNmae) 指定提交到另一个action
小结:所有的转发都不需要写参数,只写跳转的路径
所有的重定向都需要写参数,并且知道指定跳转路径
<action name="Login" class="com.lanou.entity.LoginAction">
<result type="chain">
<param name="actionName">second</param>
</result>
</action>
Struts2 配置文件的优先级
Struts2主要由两种配置文件,一种是Struts.xml,另一种是Struts.properties。
xml定义了Action的映射,而properties则定义了框架的属性,
struts.properties文件通常放在Web应用的WEB-INF/classes路径下。实际上,只要将该文件放在Web应用的CLASSPATH路径下,Struts 2框架就可以加载该文件。
本文主要使用了struts.action.extension,该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts 2处理。如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。
在这里演示了如何将properties文件中的属性加载到xml中。
<!-- 扩展名为例
文件优先级:struts.xml<struts.properties<web.xml
开发常用的是 struts.xml,基于标签的开发,应用灵活
-->
<!-- 静态属性 -->
<constant name="struts.action.extension" value="do"></constant>
struts.properties的配置
struts.action.extension=action,html,,php