Struts2:ResultType

Posted yy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2:ResultType相关的知识,希望对你有一定的参考价值。

本章节将继续学习struts2的返回类型的使用方法。

学习文档下载struts2 full包解压后会在doc下包含离线html文档。

点击运行后页面:

点击Guides向导终将会有向导列表

再点开后,就可以看到struts2支持的所有resultType:

默认的resultType是dispatcher.

常用的dispatcher,redirect,redirectAction,chain.

 1 package name="default" namespace="/" extends="struts-default">
 2         <action name="testServletObject"
 3             class="com.dx.actions.TestActionContextFetchServletObjectAction">
 4             <result>/WEB-INF/test-context.jsp</result>
 5         </action>
 6         <!-- default class="com.opensymphony.xwork2.ActionSupport" method="execute" -->
 7         <action name="gotoLoginPage">
 8         <!-- name default value:"success" -->
 9             <result>/login.jsp</result>
10         </action>
11                 
12         <action name="logout" class="com.dx.actions.TestWithXXXAware"
13             method="logout">
14             <result name="logout-success">/login.jsp</result>
15         </action>
16         
17         <action name="login" class="com.dx.actions.TestWithXXXAware" method="login">
18             <result name="login-success">/WEB-INF/login-success.jsp</result>
19         </action>
20         
21         <action name="testResult" class="com.dx.actions.TestResultAction" method="test">
22             <!-- redirect到一个Action -->
23             <result name="redirectAction-success" type="redirectAction">
24                 <param name="actionName">testRdirect</param>
25                 <param name="namespace">/testNamespace</param>
26             </result>
27             <!-- redirect -->
28             <result name="redirect-success" type="redirect">/login.jsp</result>
29             <!-- dispatcher -->
30             <result name="dispatcher-success" type="dispatcher">/login.jsp</result>
31         </action>
32         
33         <action name="createAccount" class="com.dx.actions.AccountAction" method="execute">
34             <!-- chain -->
35             <result type="chain">login</result>
36         </action>
37     </package>
38     
39     <package name="testRedirectPackage" namespace="/testNamespace" extends="struts-default">
40         <action name="testRdirect" class="com.dx.actions.TestRedirectAction" method="test">
41             <result name="test-success">/WEB-INF/test-success.jsp</result>
42         </action>
43     </package>

TestRedirectAction.java

 1 package com.dx.actions;
 2 
 3 import org.apache.struts2.ServletActionContext;
 4 
 5 public class TestRedirectAction {
 6     public String test() {
 7         String resultType = ServletActionContext.getRequest().getParameter("resultType");
 8         System.out.println(resultType);
 9         ServletActionContext.getRequest().setAttribute("resultType", resultType);
10         System.out.println("TestRedirectAction.test");
11         return "test-success";
12     }
13 }

TestResultAction.java

 1 package com.dx.actions;
 2 
 3 import org.apache.struts2.ServletActionContext;
 4 
 5 import com.opensymphony.xwork2.ActionContext;
 6 
 7 import sun.nio.cs.ext.TIS_620;
 8 
 9 public class TestResultAction {
10     private String resultType;
11 
12     public void setResultType(String resultType) {
13         this.resultType = resultType;
14     }
15 
16     public String test() {
17         System.out.println(resultType);
18 
19         if (resultType.equalsIgnoreCase("dispatcher")) {
20             System.out.println("result dispatcher-success");
21             return "dispatcher-success";
22         } else if (resultType.equalsIgnoreCase("redirect")) {
23             System.out.println("result redirect-success");
24             return "redirect-success";
25         } else if (resultType.equalsIgnoreCase("redirectAction")) {
26             System.out.println("result redirectAction-success");
27             return "redirectAction-success";
28         } else if (resultType.equalsIgnoreCase("chain")) {
29             System.out.println("result chain-success");
30             return "chain-success";
31         } else {
32             System.out.println("no match");
33         }
34         
35         ServletActionContext.getRequest().setAttribute("resultType", this.resultType);
36         
37         return "";
38     }
39 }

AccountAction.java

1 package com.dx.actions;
2 
3 public class AccountAction {
4     public String execute(){
5         System.out.println("acction action chain");
6         return "success";
7     }
8 }

index.jsp

<a href="testResult?resultType=redirectAction">redirectAction</a>
    <br />
    <a href="testResult?resultType=redirect">redirect</a>
    <br />
    <a href="testResult?resultType=dispatcher">dispatcher</a>    
    <br />
    <a href="createAccount">test chain</a>
    

result配置还有很多需要注意的地方,可以参考:

file:///D:/Java_Study/struts-2.3.28-all/struts-2.3.28/docs/docs/result-configuration.html

这里就简单说明,介绍,一遍自己可以学习使用。

以上是关于Struts2:ResultType的主要内容,如果未能解决你的问题,请参考以下文章

struts2乱码

[原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等(代码片段

Struts2 下载文件

框架整合-easyui和struts2

struts2文件下载

Struts2的那些小细节