struts2 中<result>标签中的type 属性含义总结

首先看一下在struts-default.xml中对于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>

chain:用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息.

dispatcher:用来转向页面,通常处理JSP.

freemaker:处理FreeMarker模板.

httpheader:控制特殊HTTP行为的结果类型.

redirect:重定向到一个URL,被跳转的页面中丢失传递的信息,如request.

redirectAction:重定向到一个Action,跳转的页面中丢失传递的信息.

stream:向浏览器发送InputSream对象,通常用来处理文件下载,还可用于返回AJAX数据.

velocity:处理Velocity模板.

xslt:处理XML/XLST模板.

plainText:显示原始文件内容,例如文件源代码.


重点说一下redirect和redirectAction的区别:

(1)使用redirect需要后缀名,使用redirect-action可以不需要后缀名.

(2)type="redirect"的值可以转到其它命名空间下的action,而redirect-action只能转到同一命名空下的action,因此它可以省略.do的后缀直接写action的名称.

原文地址:https://www.cnblogs.com/zlzlzl1993/p/3336777.html