您的位置:首页 > 编程语言 > Java开发

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

2016-01-28 17:25 288 查看
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代码


<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传参

Xml代码


<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 ?名称=值)。

Xml代码


<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会找不到资源。

Xml代码


<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.

Xml代码


<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

Xml代码


<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:

Xml代码


<mime-mapping>

<extension>pdf</extension>

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

</mime-mapping>

struts.xml:

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);

Xml代码


<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输出。

Xml代码


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

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

</result>

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

Xml代码


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

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

Xml代码


<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

Xml代码


.....

<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>

Xml代码


<listener>

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

</listener>

Xml代码


<dependency>

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

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

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

<scope>compile</scope>

</dependency>

Xml代码


<%@ 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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struts2 result type