Struts2支持的不同类型的返回结果为:

名字

说明

Chain Result

用来处理Action链

Dispatcher Result

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

FreeMarker Result

处理FreeMarker模板

HttpHeader Result

用来控制特殊的Http行为

Redirect Result

重定向到一个URL

Redirect Action Result

重定向到一个Action

Stream Result

向浏览器发送InputSream对象,通常用来处理文件下载

Velocity Result

处理Velocity模板

XLS Result

处理XML/XLST模板

PlainText Result

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

S2PLUGINS:Tiles Result

结合Tile使用

另外第三方的Result类型还包括JasperReports Plugin,专门用来处理JasperReport类型的报表输出。

文档中对这些Result Types是这样定义的:
1.chain
这个结果调用其他action,完成它自己定义的拦截器堆栈和结果。只能请求action,如果请求视图资源会报错。需要注意的就是与 redirect的区别,请求转发是还在当前请求,而redirect会响应一次浏览器然后浏览器再根据响应请求重定向的资源,注意看url的变化就明白 了!

Xml代码  Struts2配置精要之Result Types (Struts2.3.4)_struts2  result type

  1. <package name="public" extends="struts-default">  

  2.     <!-- Chain creatAccount to login, using the default parameter -->  

  3.     <action name="createAccount" class="...">  

  4.         <result type="chain">login</result>  

  5.     </action>  

  6.   

  7.     <action name="login" class="...">  

  8.         <!-- Chain to another namespace -->  

  9.         <result type="chain">  

  10.             <param name="actionName">dashboard</param>  

  11.             <param name="namespace">/secure</param>  

  12.         </result>  

  13.     </action>  

  14. </package>  

  15.   

  16. <package name="secure" extends="struts-default" namespace="/secure">  

  17.     <action name="dashboard" class="...">  

  18.         <result>dashboard.jsp</result>  

  19.     </action>  

  20. </package>  



2.redirectAction
重定向至Action,完成与它自己的拦截器堆栈和结果。相对于redirect来说,redirectAction只能请求action,如果 请求视图资源会报错,然后还有个小区别就是redirectAction会为url添加.action后缀而redirect不会,但是两者都可以通过 url传参

Xml代码  Struts2配置精要之Result Types (Struts2.3.4)_struts2  result type

  1. <package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">  

  2.    <!-- Pass parameters (reportType, width and height) -->  

  3.    <!--  

  4.    The redirectAction url generated will be :  

  5.    /genReport/generateReport.action?reportType=pie&width=100&height=100#summary  

  6.    -->  

  7.    <action name="gatherReportInfo" class="...">  

  8.       <result name="showReportResult" type="redirectAction">  

  9.          <param name="actionName">generateReport</param>  

  10.          <param name="namespace">/genReport</param>  

  11.          <param name="reportType">pie</param>  

  12.          <param name="width">100</param>  

  13.          <param name="height">100</param>  

  14.          <param name="empty"></param>  

  15.          <param name="suppressEmptyParameters">true</param>  

  16.          <param name="anchor">summary</param>  

  17.       </result>  

  18.    </action>  

  19. </package>  



3.redirect
让客户端请求另外的网络资源,可以为action,也可以为视图资源。
文档上是这么解释的:
调用{ @link HttpServletResponse # sendRedirect(String)sendRedirect }方法到指定的地址。 响应是告诉重定向浏览器到指定的地址(一个新的请求从客户端)。这样做的结果意味着action(action instance, action errors, field errors等)只是执行失败,不再可用。这是因为action是一个单线程模式(single-thread model)。唯一的传参方法是通过会话或用OGNL表达式,url参数(url ?名称=值)。

Xml代码  Struts2配置精要之Result Types (Struts2.3.4)_struts2  result type

  1. <package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">  

  2.    <-- Pass parameters (reportType, width and height) -->  

  3.    <!--  

  4.    The redirect-action url generated will be :  

  5.    /genReport/generateReport.jsp?reportType=pie&width=100&height=100#summary  

  6.    -->  

  7.    <action name="gatherReportInfo" class="...">  

  8.       <result name="showReportResult" type="redirect">  

  9.          <param name="location">generateReport.jsp</param>  

  10.          <param name="namespace">/genReport</param>  

  11.          <param name="reportType">pie</param>  

  12.          <param name="width">100</param>  

  13.          <param name="height">100</param>  

  14.          <param name="anchor">summary</param>  

  15.       </result>  

  16.    </action>  

  17. </package>  



4.dispatcher(缺省值,如果没有配置类型默认就是dispatcher)
包括或转发到一个视图(通常是一个jsp)。在后台Struts2将使用一个RequestDispatcher,目标servlet/JSP接 收相同的request/response对象作为原始的servlet或JSP。 因此,可以使用request.setAttribute()传递数据- - - Struts的action是可用的。如果请求action会找不到资源。

Xml代码  Struts2配置精要之Result Types (Struts2.3.4)_struts2  result type

  1. <result name="success" type="dispatcher">  

  2.   <param name="location">foo.jsp</param>  

  3. </result>  



5.httpheader
可以通过设置HTTP headers和status的值来发送错误信息给客户端。
他的参数有这些:
status - the http servlet response status code that should be set on a response.
parse - true by default. If set to false, the headers param will not be parsed for Ognl expressions.
headers - header values.
error - the http servlet response error code that should be set on a response.
errorMessage - error message to be set on response if 'error' is set.

Xml代码  Struts2配置精要之Result Types (Struts2.3.4)_struts2  result type

  1. <result name="success" type="httpheader">  

  2.   <param name="status">204</param>  

  3.   <param name="headers.a">a custom header value</param>  

  4.   <param name="headers.b">another custom header value</param>  

  5. </result>  

  6.   

  7. <result name="proxyRequired" type="httpheader">  

  8.   <param name="error">305</param>  

  9.   <param name="errorMessage">this action must be accessed through a prozy</param>  

  10. </result>  



6.stream
这个返回类型主要用作下载文件或者在浏览器上显示PDF等文档
他的参数有这些:
contentType - the stream mime-type as sent to the web browser (default = text/plain).
contentLength - the stream length in bytes (the browser displays a progress bar).
contentDisposition - the content disposition header value for specifing the file name (default = inline, values are typically p_w_upload;filename="document.pdf".
inputName - the name of the InputStream property from the chained action (default = inputStream).
bufferSize - the size of the buffer to copy from input to output (default = 1024).
allowCaching if set to 'false' it will set the headers 'Pragma' and 'Cache-Control' to 'no-cahce', and prevent client from caching the content. (default = true)
contentCharSet if set to a string, ';charset=value' will be added to the content-type header, where value is the string set. If set to an expression, the result of evaluating the expression will be used. If not set, then no charset will be set on the header

Xml代码  Struts2配置精要之Result Types (Struts2.3.4)_struts2  result type

  1. <result name="success" type="stream">  

  2.   <param name="contentType">p_w_picpath/jpeg</param>  

  3.   <param name="inputName">p_w_picpathStream</param>  

  4.   <param name="contentDisposition">p_w_upload;filename="document.pdf"</param>  

  5.   <param name="bufferSize">1024</param>  

  6. </result>  


此处给一个显示PDF文档示例:
web.xml:

Xml代码  Struts2配置精要之Result Types (Struts2.3.4)_struts2  result type

  1. <mime-mapping>      

  2.      <extension>pdf</extension>      

  3.      <mime-type>application/pdf</mime-type>      

  4. </mime-mapping>      


struts.xml:

Xml代码  Struts2配置精要之Result Types (Struts2.3.4)_struts2  result type

  1. <action name="test" class="com.iss.action.TestAction">    

  2.     <result name="success" type="stream">    

  3.        <param name="contentType">application/pdf</param>    

  4.        <param name="inputName">inputStream</param>    

  5.        <param name="contentDisposition">filename="a.pdf"</param>    

  6.     </result>    

  7. </action>   



7.plainText
响应以plain形式返回给客户端,相当于response.setContentType("text/plain; charset="+charSet);

Xml代码  Struts2配置精要之Result Types (Struts2.3.4)_struts2  result type

  1. <action name="displayJspRawContent" >  

  2.   <result type="plainText">/myJspFile.jsp</result>  

  3. </action>  

  4.   

  5. <action name="displayJspRawContent" >  

  6.   <result type="plainText">  

  7.      <param name="location">/myJspFile.jsp</param>  

  8.      <param name="charSet">UTF-8</param>  

  9.   </result>  

  10. </action>  




8.velocity
使用Servlet容器的JspFactory,这个结果模拟一个JSP执行环境,然后显示一个Velocity模板,将直接传输到Servlet输出。

Xml代码  Struts2配置精要之Result Types (Struts2.3.4)_struts2  result type

  1. <result name="success" type="velocity">  

  2.   <param name="location">foo.vm</param>  

  3. </result>  




9.freemarker
呈现一个视图使用Freemarker模板引擎。。

Xml代码  Struts2配置精要之Result Types (Struts2.3.4)_struts2  result type

  1. <result name="success" type="freemarker">foo.ftl</result>  



10.xslt
调用一个xslt文件并解析执行。

Xml代码  Struts2配置精要之Result Types (Struts2.3.4)_struts2  result type

  1. <result name="success" type="xslt">  

  2.   <param name="location">foo.xslt</param>  

  3.   <param name="matchingPattern">^/result/[^/*]$</param>  

  4.   <param name="excludingPattern">.*(hugeCollection).*</param>  

  5. </result>  



11.tiles
Tiles是一个模板框架被设计来轻松地允许创建web应用程序的页面具有一致的外观和感觉。它可以用于页面和组件化装饰。
特性:支持在Freemarker,JSP,Velocity使用Tiles

Xml代码  Struts2配置精要之Result Types (Struts2.3.4)_struts2  result type

  1. .....  

  2. <result-types>  

  3.   <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>  

  4. </result-types>  

  5. .....  

  6. <action name="sample" class="org.apache.struts2.tiles.example.SampleAction" >  

  7.   <result name="success" type="tiles">tilesWorks</result>  

  8. </action>  



Xml代码  Struts2配置精要之Result Types (Struts2.3.4)_struts2  result type

  1. <listener>  

  2.   <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>  

  3. </listener>  



Xml代码  Struts2配置精要之Result Types (Struts2.3.4)_struts2  result type

  1. <dependency>  

  2.   <groupId>org.apache.struts</groupId>  

  3.   <artifactId>struts2-tiles-plugin</artifactId>  

  4.   <version>${version.tiles}</version>  

  5.   <scope>compile</scope>  

  6. </dependency>  



Xml代码  Struts2配置精要之Result Types (Struts2.3.4)_struts2  result type

  1. <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>  

  2. <%@ taglib prefix="s" uri="/struts-tags" %>  

  3.   

  4. <%-- Show usage; Used in Header --%>  

  5. <tiles:importAttribute name="title" scope="request"/>  

  6. <html>  

  7.     <head><title><tiles:getAsString name="title"/></title></head>  

  8. <body>  

  9.     <tiles:insertAttribute name="header"/>  

  10.   

  11.     <p id="body">  

  12.         <tiles:insertAttribute name="body"/>  

  13.     </p>  

  14.   

  15.     <p>Notice that this is a layout made in JSP</p>  

  16. </body>  

  17. </html>