struts2 result type默认支持的类型有:
<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
</result-types>
下面就最常用的几个总结如下:
1.redirct
<package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
<-- Pass parameters (reportType, width and height) -->
<!--
The redirect-action url generated will be :
/genReport/generateReport.jsp?reportType=pie&width=100&height=100#summary
-->
<action name="gatherReportInfo" class="...">
<result name="showReportResult" type="redirect">
<param name="location">generateReport.jsp</param>
<param name="namespace">/genReport</param>
<param name="reportType">pie</param>
<param name="width">100</param>
<param name="height">100</param>
<param name="anchor">summary</param>
</result>
</action>
</package>
这里是struts2参考文档上的一段代码,使用方法已经相当明了,这里:
1.param可以使用OGNL来取当前action中的值,例如 <param name="width">${width}</param>
2.namespace 属性貌似有bug,我测试总是不能起作用,可以直接将namespace加在location中
2.chain
注:
1.action依次被压入到值栈
2.actioncontext不共享,因为actioncontext是记录action运行过程中所需要的数据的,依次在上个action中放入到actioncontext中的数据在下一个actioncontext中是去不到值得
3.由于OGNL查找值得过程从栈顶向下找,如果找到了就返回,如果需要取得上一个action中的值,需要使用 [1].xxx的形式, [1] 表示从第2层的action来取值,即上一个action。