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" extends="struts-default" namespace="/secure"> <action name="dashboard" name="code"><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" 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>
<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" 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>
<result name="success" type="dispatcher"> <param name="location">foo.jsp</param></result>
<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>
<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>
<mime-mapping> <extension>pdf</extension> <mime-type>application/pdf</mime-type> </mime-mapping>
<action name="test" type="stream"> <param name="contentType">application/pdf</param> <param name="inputName">inputStream</param> <param name="contentDisposition">filename="a.pdf"</param> </result> </action>
<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>
<result name="success" type="velocity"> <param name="location">foo.vm</param></result>
<result name="success" type="freemarker">foo.ftl</result>
<result name="success" type="xslt"> <param name="location">foo.xslt</param> <param name="matchingPattern">^/result/[^/*]$</param> <param name="excludingPattern">.*(hugeCollection).*</param></result>
.....<result-types> <result-type name="tiles" > <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>