可以在struts2-core-{version}.jar中找到struts-default.xml,里面列举了当前可以使用的所有result-type以及对应的class
此处是struts2.2.3的
1. <result-types>
2. <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
3. <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
4. <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
5. <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
6. <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
7. <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
8. <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
9. <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
10. <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
11. <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
12. </result-types>
1.chain
该类型是请求转发给其他的action,如果为jsp则会报错
需要注意的就是与redirect的区别,请求转发是还在当前请求,
而redirect会响应一次浏览器然后浏览器再根据响应请求重定向的资源,注意看url的变化就明白了!
下面是apache给的示例,这个比较简单,没什么说的,
1. <package name="public" extends="struts-default">
2. <!-- Chain creatAccount to login, using the default parameter -->
3. <action name="createAccount" class="...">
4. <result type="chain">login</result>
5. </action>
6.
7. <action name="login" class="...">
8. <!-- Chain to another namespace -->
9. <result type="chain">
10. <param name="actionName">dashboard</param>
11. <param name="namespace">/secure</param>
12. </result>
13. </action>
14. </package>
15.
16. <package name="secure" extends="struts-default" namespace="/secure">
17. <action name="dashboard" class="...">
18. <result>dashboard.jsp</result>
19. </action>
20. </package>
2.dispatcher 请求转发视图资源,比如jsp,html,如果请求action会找不到资源, 请求转发的问题看1中的说明 apache给的示例。。也没啥说的
1. <result name="success" type="dispatcher">
2. <param name="location">foo.jsp</param>
3. </result>
3.httpheader 这个挺有意思的,可以随便更改响应状态 比如下面的这个示例
1. <action name="test" class="com.iss.action.TestAction">
2. <result name="success" type="httpheader">
3. <param name="status">400</param>
4. </result>
5. </action>
当action返回SUCCESS的时候,会将响应状态修改为400,客户端错误的请求, 还有其他的状态可以自行尝试,比如为100时,浏览器会在请求一段时间之后继续当前页面,500则为服务器内部错误等等 具体为什么会产生这样的结果,当然得把源码翻出来了 下面是HttpHeaderResult的一些成员变量,private的可以作为param的name属性
1. private static final long serialVersionUID = 195648957144219214L;
2.
3. /** The default parameter */
4. public static final String DEFAULT_PARAM = "status";
5.
6.
7. private boolean parse = true;
8. private Map<String,String> headers;
9. private int status = -1;
10. private int error = -1;
11. private String errorMessage;
下面是具体执行操作过程,可以看到response.setStatus等操作
1. public void execute(ActionInvocation invocation) throws Exception {
2. HttpServletResponse response = ServletActionContext.getResponse();
3. ValueStack stack = ActionContext.getContext().getValueStack();
4.
5. if (status != -1) {
6. response.setStatus(status);
7. else if (error != -1) {
8. if (errorMessage != null) {
9. String finalMessage = parse ? TextParseUtil.translateVariables(
10. errorMessage, stack) : errorMessage;
11. response.sendError(error, finalMessage);
12. else
13. response.sendError(error);
14. }
15.
16. if (headers != null) {
17. for (Iterator iterator = headers.entrySet().iterator();
18. iterator.hasNext();) {
19. Map.Entry entry = (Map.Entry) iterator.next();
20. String value = (String) entry.getValue();
21. String finalValue = parse ? TextParseUtil.translateVariables(value, stack) : value;
22. response.addHeader((String) entry.getKey(), finalValue);
23. }
24. }
25. }
4.redirect
重定向操作,与请求转发的区别看1的介绍
让客户端请求另外的网络资源,可以为action,也可以为视图资源
下面是apache的示例
1. <result name="success" type="redirect">
2. <param name="location">foo.jsp</param>
3. <param name="parse">false</param>
4. </result>
5.redirectAction
重定向至Action,与redirect的区别找了好久,但是也没发现比较权威的说明
最明显的区别当然是redirectAction只能请求action,如果请求视图资源会报错
然后还有个小区别就是redirectAction会为url添加.action后缀而redirect不会
有说redirectAction无法通过url传参,但是我测试时完全可以通过request获取到,
此处区别如果有哪位知道的麻烦告知,Thank you!
下面是apache的传参的示例
1. <package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
2. <-- Pass parameters (reportType, width and height) -->
3. <!--
4. The redirect-action url generated will be :
5. reportType=pie&width=100&height=100
6. >
7. <action name="gatherReportInfo" class="...">
8. <result name="showReportResult" type="redirect-action">
9. <param name="actionName">generateReport</param>
10. <param name="namespace">/genReport</param>
11. <param name="reportType">pie</param>
12. <param name="width">100</param>
13. <param name="height">100</param>
14. </result>
15. </action>
16. </package>
6.stream
这个返回类型主要用作下载文件或者在浏览器上显示PDF等文档
此处给一个显示PDF文档示例
项目web.xml中
1. <mime-mapping>
2. <extension>pdf</extension>
3. <mime-type>application/pdf</mime-type>
4. </mime-mapping>
struts.xml中
1. <action name="test" class="com.iss.action.TestAction">
2. <result name="success" type="stream">
3. <param name="contentType">application/pdf</param>
4. <param name="inputName">inputStream</param>
5. <param name="contentDisposition">filename="a.pdf"</param>
6. </result>
7. </action>
TestAction.java中
1. public class TestAction extends ActionSupport
2. {
3.
4. private InputStream inputStream;
5.
6.
7. public InputStream getInputStream()
8. {
9. return inputStream;
10. }
11.
12. @Override
13. public String execute() throws Exception
14. {
15. new FileInputStream("xxxxx/a.pdf");//要下载或显示的文件路径
16. return SUCCESS;
17. }
18. }
需要注意的地方是struts.xml中inputName的值要与TestAction中的属性名相同,在此处就是inputStream
7.plainText
响应以plain形式返回给客户端
截取源码的最重要的一部分
1. if (charset != null) {
2. "text/plain; charset="+charSet);
3. }
4. else {
5. "text/plain");
6. }
8.freemarker,velocity,xslt
没有尝试过,有兴趣的可以尝试下
引用通告地址: http://tmsoft.lsxy.com/trackback.php?tbID=992&extra=010228