struts2学习笔记【八】/ struts.xml--action
1.?action详解
Action自身的设置在package中我们已经讲解过,在这里就不重复叙述了,我们对action中的其他信息进行学习。
1.1.?result action处理结果组件
name result的名称,同action类处理后的返回值是对应的。
type result的处理类型
一般情况下最简单的配置result只要设置一个name属性就行了,type属性书负责处理客户端跳转还是服务器端跳转的。
客户端发出请求时,我们有时候不允许页面直接跳转到页面,那么就需要进行如下设置:
<action?name="main">???????//没有指定action类,只是一个单纯的转向
<result>/main.jsp</result>
</action>
前台url访问,main.action,但是url中是服务器端跳转,这个没有任何的意义,所以我们需要修改如下:
<result?type="redirect">/main.jsp</result>
Type的类型有两种:
a)chain com.opensymphony.xwork2.ActionChainResult
b)redirectAction org.apache.struts2.dispatcher.ServletActionRedirectResult
以上两种方式均可以跳转到action中,前者是服务器端跳转,后者是客户端跳转,表现在地址栏上的信息会发生变化,但是无论是前者还是后者,单纯的action之间的跳转,在最终的页面上均能获得前面action中传递的属性值【拦截器捕获传递】。
<action>
//A)错
<result??name?="success">/test.action</result>
//B)错
<result??name?="success">test.action</result>
//C)错
<result??name?="success">test</result>
//D)服务器端action跳转
<result??name?="success"??type="chain">
<param?name="actionName">test</param>
</result>
//E)客户端跳转
<result??name?="success"??type="redirectAction">
<param?name="actionName">test</param>
</result>
</action>
以上几种配置中A,B,C的写法是错误的,因为我们采用的default模式,其完整的写法应该是:
<result??name?="success"??type="dispatcher">
<param?name="Location">test</param>
</result>
其中param的name属性中的值都是既定的,不能自定义。
D,E两种配置方式中,跳转后传递的属性值还是可以获取的,但是如果在指向的action中设置了其结果指向<result??type="redirect"></result>,那么前一个action中的属性值在无法在最终跳转的页面上获取了,因为页面已经转向客户端跳转了。
<result??name?="success"??type="redirect">/test.jsp</result>
但是我必须要进行客户端跳转,同时又想获得传递的属性值怎么办呢?
传参:/test.jsp?name=${username}
同样的,在页面上取值时也要适当变化了,${param.name}
如果不支持EL表达式,可能是版本问题,需要修改java?compiler为5.0
注意:action到action之间的跳转时,如果两个action不在同一个包下,则需要加上对应的命名空间:
<result??name="success"??type="redirectAction">
<param??name="actionName">/命名空间名/test</param>
或者是
<param??name="actionName">/test</param>
<param??name="namespace">/命名空间名</param>
</result>
页面转向的动态结果:
<action??name="success"??type="chain">${newActionName}</result>
补充:XSLT【处理XML/XLST模版】
plainTest?显示原始文件的内容,例如文件源码??org.apache.struts2.dispatcher.PlainTextResult
Location:?目标文件
Charset: ?字符编码
例如:
<action??name?=?"success"?type="plainText">
<param??name="Location">/test.jsp</param>
<param??name="charset">GBK</param>
</action>
1.2.?interceptor-ref 拦截器引用
1.3.?param 参数
1.4.?exception-mapping 异常处理
异常的使用需要拦截器的协助,在后台捕获到异常时,若struts.xml中有相应的异常配置,那么拦截器就会跳转到指定页面,同时输出错误信息.
<result?name="exception">/exception.jsp</result>
<exception-mapping
?result="exception"?exception="java.sql.SQLException"?/>
<!--EndFragment-->