本章节将继续学习struts2的返回类型的使用方法。
学习文档下载struts2 full包解压后会在doc下包含离线html文档。
点击运行后页面:
点击Guides向导终将会有向导列表
再点开后,就可以看到struts2支持的所有resultType:
默认的resultType是dispatcher.
常用的dispatcher,redirect,redirectAction,chain.
package name="default" namespace="/" extends="struts-default">
<action name="testServletObject"
class="com.dx.actions.TestActionContextFetchServletObjectAction">
<result>/WEB-INF/test-context.jsp</result>
</action>
<!-- default class="com.opensymphony.xwork2.ActionSupport" method="execute" -->
<action name="gotoLoginPage">
<!-- name default value:"success" -->
<result>/login.jsp</result>
</action> <action name="logout" class="com.dx.actions.TestWithXXXAware"
method="logout">
<result name="logout-success">/login.jsp</result>
</action> <action name="login" class="com.dx.actions.TestWithXXXAware" method="login">
<result name="login-success">/WEB-INF/login-success.jsp</result>
</action> <action name="testResult" class="com.dx.actions.TestResultAction" method="test">
<!-- redirect到一个Action -->
<result name="redirectAction-success" type="redirectAction">
<param name="actionName">testRdirect</param>
<param name="namespace">/testNamespace</param>
</result>
<!-- redirect -->
<result name="redirect-success" type="redirect">/login.jsp</result>
<!-- dispatcher -->
<result name="dispatcher-success" type="dispatcher">/login.jsp</result>
</action> <action name="createAccount" class="com.dx.actions.AccountAction" method="execute">
<!-- chain -->
<result type="chain">login</result>
</action>
</package> <package name="testRedirectPackage" namespace="/testNamespace" extends="struts-default">
<action name="testRdirect" class="com.dx.actions.TestRedirectAction" method="test">
<result name="test-success">/WEB-INF/test-success.jsp</result>
</action>
</package>
TestRedirectAction.java
package com.dx.actions; import org.apache.struts2.ServletActionContext; public class TestRedirectAction {
public String test() {
String resultType = ServletActionContext.getRequest().getParameter("resultType");
System.out.println(resultType);
ServletActionContext.getRequest().setAttribute("resultType", resultType);
System.out.println("TestRedirectAction.test");
return "test-success";
}
}
TestResultAction.java
package com.dx.actions; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import sun.nio.cs.ext.TIS_620; public class TestResultAction {
private String resultType; public void setResultType(String resultType) {
this.resultType = resultType;
} public String test() {
System.out.println(resultType); if (resultType.equalsIgnoreCase("dispatcher")) {
System.out.println("result dispatcher-success");
return "dispatcher-success";
} else if (resultType.equalsIgnoreCase("redirect")) {
System.out.println("result redirect-success");
return "redirect-success";
} else if (resultType.equalsIgnoreCase("redirectAction")) {
System.out.println("result redirectAction-success");
return "redirectAction-success";
} else if (resultType.equalsIgnoreCase("chain")) {
System.out.println("result chain-success");
return "chain-success";
} else {
System.out.println("no match");
} ServletActionContext.getRequest().setAttribute("resultType", this.resultType); return "";
}
}
AccountAction.java
package com.dx.actions; public class AccountAction {
public String execute(){
System.out.println("acction action chain");
return "success";
}
}
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
这里就简单说明,介绍,一遍自己可以学习使用。