视图返回类型详细的信息可以查看 struts2-core-2.1.8.1.jar 包下的 struts-default.xml 里,这个我在拦截器那块已经贴出来了。下面我们就简单介绍各自的作用:
类型 |
描述 |
使用的类 |
chain |
用来处理Action 链,被跳转的action 中仍能获取上个页面的值,如request 信息。 |
com.opensymphony.xwork2.ActionChainResult |
dispatcher |
用来转向页面,通常处理JSP |
org.apache.struts2.dispatcher.ServletDispatcherResult |
freemaker |
处理FreeMarker 模板 |
org.apache.struts2.views.freemarker.FreemarkerResult |
httpheader |
控制特殊HTTP 行为的结果类型 |
org.apache.struts2.dispatcher.HttpHeaderResult |
stream |
向浏览器发送InputSream 对象,通常用来处理文件下载,还可用于返回AJAX 数据 |
org.apache.struts2.dispatcher.StreamResult |
velocity |
处理Velocity 模板 |
org.apache.struts2.dispatcher.VelocityResult |
xslt |
处理XML/XLST 模板 |
org.apache.struts2.views.xslt.XSLTResult |
plainText |
显示原始文件内容,例如文件源代码 |
org.apache.struts2.dispatcher.PlainTextResult |
redirect |
重定向到一个URL ,被跳转的页面中丢失传递的信息,如request |
org.apache.struts2.dispatcher.ServletRedirectResult |
redirectAction |
重定向到一个Action ,跳转的页面中丢失传递的信息,如request |
org.apache.struts2.dispatcher.ServletActionRedirectResult |
1 、 Chain:
<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 、 Dispatcher :
<result name="success" type="dispatcher"> <param name="location">foo.jsp</param> </result>
3 、 FreeMarker :
<result name="success" type="freemarker">foo.ftl</result>
4 、 HttpHeader :
<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>
5 、 Redirect :
<result name="success" type="redirect"> <param name="location">foo.jsp</param> <param name="parse">false</param> <param name="anchor">FRAGMENT</param> </result>
<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 --> <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> </result> </action> </package>
6 、 Redirect Action :
<package name="public" extends="struts-default"> <action name="login" class="..."> <!-- Redirect to another namespace --> <result type="redirectAction"> <param name="actionName">dashboard</param> <param name="namespace">/secure</param> </result> </action> </package> <package name="secure" extends="struts-default" namespace="/secure"> <-- Redirect to an action in the same namespace --> <action name="dashboard" class="..."> <result>dashboard.jsp</result> <result name="error" type="redirectAction">error</result> </action> <action name="error" class="..."> <result>error.jsp</result> </action> </package> <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 --> <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="supressEmptyParameters">true</param> </result> </action> </package>
7 、 Stream :
<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>
8 、 Velocity :
<result name="success" type="velocity"> <param name="location">foo.vm</param> </result>
9 、 XSL :
<result name="success" type="xslt"> <param name="location">foo.xslt</param> <param name="matchingPattern">^/result/[^/*]$</param> <param name="excludingPattern">.*(hugeCollection).*</param> </result>
10 、 PlainText :
<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>
11 、 Tiles :
<%@ 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>