Struts2配置精要之Result Types(Struts2.3.4)

struts2.3.4的Predefined Result Types,比struts2.2.3要多出一种:
Chain                 Used for Action Chaining
Dispatcher         Used for web resource integration, including JSP integration
FreeMarker         Used for FreeMarker integration
HttpHeader         Used to control special HTTP behaviors
Redirect         Used to redirect to another URL (web resource)
RedirectAction         Used to redirect to another action mapping
Stream                  Used to stream an InputStream back to the browser(usually for file downloads)
Velocity         Used for Velocity integration
XSL                 Used for XML/XSLT integration
PlainText         Used to display the raw content of a particular page(i.e jsp, HTML)
Tiles                 Used to provide Tiles integration

文档中对这些Result Types是这样定义的:
1.chain
这个结果调用其他action,完成它自己定义的拦截器堆栈和结果。只能请求action,如果请求视图资源会报错。需要注意的就是与redirect的区别,请求转发是还在当前请求,而redirect会响应一次浏览器然后浏览器再根据响应请求重定向的资源,注意看url的变化就明白了!
<package name="public" extends="struts-default">
    <!-- Chain creatAccount to login, using the default parameter -->
    <action name="createAccount" class="...">
        <result type="chain">login</result>
    </action>

    <action name="login" class="...">
        <!-- Chain to another namespace -->
        <result type="chain">
            <param name="actionName">dashboard</param>
            <param name="namespace">/secure</param>
        </result>
    </action>
</package>

<package name="secure" extends="struts-default" namespace="/secure">
    <action name="dashboard" class="...">
        <result>dashboard.jsp</result>
    </action>
</package>


2.redirectAction
重定向至Action,完成与它自己的拦截器堆栈和结果。相对于redirect来说,redirectAction只能请求action,如果请求视图资源会报错,然后还有个小区别就是redirectAction会为url添加.action后缀而redirect不会,但是两者都可以通过url传参
<package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
   <!-- Pass parameters (reportType, width and height) -->
   <!--
   The redirectAction url generated will be :
   /genReport/generateReport.action?reportType=pie&width=100&height=100#summary
   -->
   <action name="gatherReportInfo" class="...">
      <result name="showReportResult" type="redirectAction">
         <param name="actionName">generateReport</param>
         <param name="namespace">/genReport</param>
         <param name="reportType">pie</param>
         <param name="width">100</param>
         <param name="height">100</param>
         <param name="empty"></param>
         <param name="suppressEmptyParameters">true</param>
         <param name="anchor">summary</param>
      </result>
   </action>
</package>


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


4.dispatcher(缺省值,如果没有配置类型默认就是dispatcher)
包括或转发到一个视图(通常是一个jsp)。在后台Struts2将使用一个RequestDispatcher,目标servlet/JSP接收相同的request/response对象作为原始的servlet或JSP。 因此,可以使用request.setAttribute()传递数据- - - Struts的action是可用的。如果请求action会找不到资源。
<result name="success" type="dispatcher">
  <param name="location">foo.jsp</param>
</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.
<result name="success" type="httpheader">
  <param name="status">204</param>
  <param name="headers.a">a custom header value</param>
  <param name="headers.b">another custom header value</param>
</result>

<result name="proxyRequired" type="httpheader">
  <param name="error">305</param>
  <param name="errorMessage">this action must be accessed through a prozy</param>
</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 attachment;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
<result name="success" type="stream">
  <param name="contentType">image/jpeg</param>
  <param name="inputName">imageStream</param>
  <param name="contentDisposition">attachment;filename="document.pdf"</param>
  <param name="bufferSize">1024</param>
</result>

此处给一个显示PDF文档示例:
web.xml:
<mime-mapping>    
     <extension>pdf</extension>    
     <mime-type>application/pdf</mime-type>    
</mime-mapping>

struts.xml:
<action name="test" class="com.iss.action.TestAction">  
    <result name="success" type="stream">  
       <param name="contentType">application/pdf</param>  
       <param name="inputName">inputStream</param>  
       <param name="contentDisposition">filename="a.pdf"</param>  
    </result>  
</action>


7.plainText
响应以plain形式返回给客户端,相当于response.setContentType("text/plain; charset="+charSet);
<action name="displayJspRawContent" >
  <result type="plainText">/myJspFile.jsp</result>
</action>

<action name="displayJspRawContent" >
  <result type="plainText">
     <param name="location">/myJspFile.jsp</param>
     <param name="charSet">UTF-8</param>
  </result>
</action>



8.velocity
使用Servlet容器的JspFactory,这个结果模拟一个JSP执行环境,然后显示一个Velocity模板,将直接传输到Servlet输出。
<result name="success" type="velocity">
  <param name="location">foo.vm</param>
</result>



9.freemarker
呈现一个视图使用Freemarker模板引擎。。
<result name="success" type="freemarker">foo.ftl</result>


10.xslt
调用一个xslt文件并解析执行。
<result name="success" type="xslt">
  <param name="location">foo.xslt</param>
  <param name="matchingPattern">^/result/[^/*]$</param>
  <param name="excludingPattern">.*(hugeCollection).*</param>
</result>


11.tiles
Tiles是一个模板框架被设计来轻松地允许创建web应用程序的页面具有一致的外观和感觉。它可以用于页面和组件化装饰。
特性:支持在Freemarker,JSP,Velocity使用Tiles
.....
<result-types>
  <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
</result-types>
.....
<action name="sample" class="org.apache.struts2.tiles.example.SampleAction" >
  <result name="success" type="tiles">tilesWorks</result>
</action>


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


<dependency>
  <groupId>org.apache.struts</groupId>
  <artifactId>struts2-tiles-plugin</artifactId>
  <version>${version.tiles}</version>
  <scope>compile</scope>
</dependency>


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

<%-- Show usage; Used in Header --%>
<tiles:importAttribute name="title" scope="request"/>
<html>
    <head><title><tiles:getAsString name="title"/></title></head>
<body>
    <tiles:insertAttribute name="header"/>

    <p id="body">
        <tiles:insertAttribute name="body"/>
    </p>

    <p>Notice that this is a layout made in JSP</p>
</body>
</html>

转载于:https://my.oschina.net/easyean/blog/535154

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值