-
- <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
- <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
- <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
- <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
- <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
- <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
- <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
- <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
- <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
- <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
<result name="success" type="dispatcher">
<param name="location">/ThankYou.jsp</param>
</result>
<param name="location">/ThankYou.jsp</param>
</result>
由于type默认值是dispatcher,所以不需要定义,另外name的默认值为success。
上述代码可以简写为:
<result>
<param name="location">/ThankYou.jsp</param>
</result>
另外location参数也可以直接卸载result标签内部,所以上述代码的最简单的写法为:
<result>/ThankYou.jsp</result>
上述代码可以简写为:
<result>
<param name="location">/ThankYou.jsp</param>
</result>
另外location参数也可以直接卸载result标签内部,所以上述代码的最简单的写法为:
<result>/ThankYou.jsp</result>
1.chain
该类型是请求转发给其他的action,如果为jsp则会报错
需要注意的就是与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>
2.dispatcher 请求转发视图资源,比如jsp,html
- <result name="success" type="dispatcher">
- <param name="location">foo.jsp</param>
- </result>
3.httpheader 可以随便更改响应状态 参考此链接
- <action name="test" class="com.iss.action.TestAction">
- <result name="success" type="httpheader">
- <param name="status">400</param>
- </result>
- </action>
4.redirect
重定向操作,让客户端请求另外的网络资源,可以为action,也可以为视图资源
- <result name="success" type="redirect">
- <param name="location">foo.jsp</param>
- <param name="parse">false</param>
- </result>
-
<!-- 重定向 参数:location:重定向的目的地 parse: 表明是否把location参数的值视为一个OGNL表达式来解释,默认值为true --> <action name="..." class="..."> <result name="success" type="redirect"> <!-- 内部资源 --> /jsp/Product.jsp <!-- Action带动态参数(${userName}值为本Action中的userName属性值) --> UserAction.action?userName=${userName} <!-- 外部资源(如果需要使用&和+之类的特殊字符必须使用转义序列.如:&改成&) http://www.google.com?user=1&site=4 转成:http://www.google.com?user=1&site=4 --> http://www.google.com </result> </action>
重定向至Action,与redirect的区别是redirectAction只能请求action,至于其他的未知
-
<!-- 重定向到一个Action 参数:actionName:指定重定向Action的名字 namespace: 指定重定向Action的命名空间(没有此参数,与本action同一个命名空间) -->
- <package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
- <-- Pass parameters (reportType, width and height) -->
- <!--
- The redirect-action url generated will be :
- /genReport/generateReport.action?reportType=pie&width=100&height=100
- -->
- <action name="gatherReportInfo" class="...">
- <result name="showReportResult" type="redirect-action">
- <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>
- </result>
- </action>
- </package>
这个返回类型主要用作下载文件或者在浏览器上显示PDF等文档
此处给一个显示PDF文档示例
项目web.xml中
- <mime-mapping>
- <extension>pdf</extension>
- <mime-type>application/pdf</mime-type>
- </mime-mapping>
- <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>
(需要注意的地方是struts.xml中inputName的值要与TestAction中的get方法名相关,如在此处是getInputStream,inputName则为inputStream)
- public class TestAction extends ActionSupport
- {
- private InputStream inputStream;
- public InputStream getInputStream()
- {
- return inputStream;
- }
- @Override
- public String execute() throws Exception
- {
- inputStream = new FileInputStream("xxxxx/a.pdf");//要下载或显示的文件路径,最好是工程底下的,至于其他的文件好像下载不下来,
- return SUCCESS;
- }
- }
7.plainText
响应以plain形式返回给客户端
<!-- 通常被用来发送JSP页面的源代码 --> <action name="source_show" class="..."> <result name="success" type="plaintext">/jsp/Menu.jsp</result> </action>