struts2中result配置的各种视图转发类型

result配置类似于struts1中的forward,但struts2中提供了多种结果类型,如: dispatcher(默认值)、redirect?、 redirectAction?、plainText。
下面是redirectAction?结果类型的例子,如果重定向的action中同一个包下:
<resulttype="redirectAction">helloworld</result>
如果重定向的action在别的命名空间下:
<result type="redirectAction">
<paramname="actionName">helloworld</param>
<paramname="namespace">/test</param>
</result>
如果没有指定result的name属性,默认值为success。
在result中还可以使用${属性名}表达式,表达式里的属性名对应action中的属性。如下:
<resulttype="redirect">view.jsp?id=${id}</result>
<package name="user"namespace="/users/userinfo" extends="struts-default">
?? <action name="helloworld" method="execute"class="com.sxt.HelloWorldAction">
??? <resultname="show">/showMessage.jsp</result>
?? </action>
?? <!--?重定向 -->
?? <action name="redirect">
??? <resulttype="redirect">/showMessage.jsp</result>
?? </action>
?? <!--?重定向到Action -->
?? <action name="redirectAction">
??? <resulttype="redirectAction">helloworld</result>
?? </action>
?? <!--?重定向到不同包的Action -->
?? <action name="redirectPackageAction">
??? <result type="redirectAction">
???? <paramname="namespace">/users/dept</param>
???? <param name="actionName">userDept</param>
??? </result>
?? </action>
?? <!--?参数传值 -->
?? <action name="paramvalue" method="userEdit"class="com.sxt.HelloWorldAction">
??? <result name="userEdit"type="redirect">/userEdit.jsp?userId=${userid}</result>
?? </action>
</package>
<package name="other" namespace="/users/dept"extends="struts-default">
?? <action name="userDept">
??? <result>/userDept.jsp</result>
?? </action>
</package>
链接类型??result:chain
总结:通常从一个action链接到另外一个action
<package name="public"extends="struts-default">???
????<!-- ChaincreatAccount to login, using the default parameter -->??
????<actionname="createAccount" class="...">???
????????<resulttype="chain">login</result>?
???</action>??
????<actionname="login" class="...">???
????????<!--Chain to another namespace -->??
????????<resulttype="chain">???
???????????<param name="actionName">dashboard</param>??
???????????<param name="namespace">/secure</param>??
???????</result>??
???</action>??
</package>??
<package name="secure" extends="struts-default"namespace="/secure">???
????<actionname="dashboard" class="...">???
???????<result>dashboard.jsp</result>??
???</action>??
</package>?
Dispatcher Result???????用来转向页面,通常处理JSP?????
<result name="success"type="dispatcher">???
??<param name="location">foo.jsp</param>??
</result>?

??FreeMarkerResult??处理FreeMarker模板
<result name="success"type="freemarker">foo.ftl</result>??
HttpHeader Result
用来控制特殊的Http行为
<result name="success"type="httpheader">???
??<paramname="status">204</param>?
??<paramname="headers.a">a custom header value</param>??
??<paramname="headers.b">another custom header value</param>??
</result>??
<result name="proxyRequired"type="httpheader">???
??<paramname="error">305</param>?
??<paramname="errorMessage">this action must be accessed through aprozy</param>??
</result>??

Redirect Result
重定向到一个URL
<result name="success"type="redirect">? ?
?? ?<param name="location">foo.jsp</param>
?? ?<param name="parse">false</param>
</result>
< class="javascript">
< class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shCore.js">
< class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushCSharp.js">
< class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushPhp.js">
< class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushJScript.js">
< class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushVb.js">
< class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushSql.js">
< class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushXml.js">
< class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushShell.js">
< class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushDelphi.js">
< class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushPython.js">
< class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushJava.js">


<package name="passingRequestParameters"extends="struts-default"namespace="/passingRequestParameters">???
???<-- Passparameters (reportType, width and height) -->???
???<!--???
???Theredirect-action url generated will be ?
??/genReport/generateReport.jsp?reportType=pie&width=100&height=100???
???-->???
???<actionname="gatherReportInfo" class="...">???
??????<resultname="showReportResult" type="redirect">???
?????????<paramname="location">generateReport.jsp</param>???
?????????<paramname="namespace">/genReport</param>???
?????????<paramname="reportType">pie</param>???
?????????<paramname="width">100</param>??
?????????<paramname="height">100</param>??
?????</result>???
??</action>???
</package>??
Redirect Action Result
重定向到一个Action
<package name="public"extends="struts-default">???
????<action name="login"class="...">???
????????<!--Redirect to another namespace -->??
????????<resulttype="redirectAction">???
???????????<param name="actionName">dashboard</param>??
???????????<param name="namespace">/secure</param>??
????????</result>??
???</action>??
</package>??
<package name="secure"extends="struts-default" namespace="/secure">???
????<-- Redirectto an action in the same namespace -->?
????<actionname="dashboard" class="...">???
???????<result>dashboard.jsp</result>??
????????<result name="error"type="redirectAction">error</result>??
???</action>??
????<actionname="error" class="...">???
???????<result>error.jsp</result>?
???</action>??
</package>??
<package name="passingRequestParameters"extends="struts-default"namespace="/passingRequestParameters">???
???<-- Passparameters (reportType, width and height) -->??
???<!--???
???TheredirectAction url generated will be ??
??/genReport/generateReport.action?reportType=pie&width=100&height=100??
???-->??
???<actionname="gatherReportInfo" class="...">???
??????<resultname="showReportResult" type="redirectAction">???
?????????<paramname="actionName">generateReport</param>??
?????????<paramname="namespace">/genReport</param>??
?????????<paramname="reportType">pie</param>??
?????????<paramname="width">100</param>?
?????????<paramname="height">100</param>?
?????????<paramname="empty"></param>?
?????????<paramname="supressEmptyParameters">true</param>??
?????</result>??
??</action>??
</package>?
Stream Result
向浏览器发送InputSream对象,通常用来处理文件下载

<result name="success"type="stream">???
??<paramname="contentType">image/jpeg</param>??
??<paramname="inputName">imageStream</param>??
??<paramname="contentDisposition">attachment;filename="document.pdf"</param>???
??<paramname="bufferSize">1024</param>??
</result>??

Velocity Result
处理Velocity模板
<result name="success"type="velocity">???
??<paramname="location">foo.vm</param>??
</result>??
?
XLS Result
处理XML/XLST模板
<result name="success" type="xslt">???
??<paramname="location">foo.xslt</param>??
??<paramname="matchingPattern">^/result/[^/*]$</param>??
??<paramname="excludingPattern">.*(hugeCollection).*</param>??
</result>??

PlainText Result
显示原始文件内容,例如文件源代码
<action name="displayJspRawContent">??
??<resulttype="plaintext">/myJspFile.jsp</result>??
</action>? ?
<action name="displayJspRawContent">??
??<resulttype="plaintext">???
?????<paramname="location">/myJspFile.jsp</param>??
?????<paramname="charSet">UTF-8</param>?
??</result>??
</action>?
S2PLUGINS:Tiles Result
结合Tile使用
另外第三方的Result类型还包括JasperReportsPlugin,专门用来处理JasperReport类型的报表输出。
<%@ tagliburi="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:getAsStringname="title"/></title></head>???
<body>??
???<tiles:insertAttribute name="header"/>???
????? <pid="body">???
????????<tiles:insertAttributename="body"/>???
????</p>??
? ? <p>Noticethat this is a layout made in JSP</p>?
</body>??
</html>?

来自: http://www.24xuexi.com/w/2011-06-29/93842.html

你可能感兴趣的:(struts2)