Result
1. 常用四种类型:
a) dispatcher(默认)
b) redirect
c) chain
d) redirectAction
2. 全局结果集
a) global-results | extends
3. 动态结果(了解)
a) 在action中保存一个属性,存储具体的结果location
4. 传递参数
a) 客户端跳转才需要传递
b) ${}表达式(不是EL)
result 类型 跳转
Result 类型
dispatcher
redirect
chain
redirectAction
freemarker
httpheader
stream
velocity
xslt
plaintext
tiles
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
</head>
<body>
Result 类型
<ol>
<li><a href="r/r1">dispatcher</a></li>
<li><a href="r/r2">redirect</a></li>
<li><a href="r/r3">chain</a></li>
<li><a href="r/r4">redirectAction</a></li>
<li>freemarker</li>
<li>httpheader</li>
<li>stream</li>
<li>velocity</li>
<li>xslt</li>
<li>plaintext</li>
<li>tiles</li>
</ol>
</body>
</html>
<package name="resultType" extends="struts-default" namespace="/r">
<action name="r1">
<result type="dispatcher">
/r1.jsp
</result>
</action>
<action name="r2">
<result type="redirect">
/r2.jsp
</result>
</action>
<action name="r3">
<result type="chain">
r1
</result>
</action>
<action name="r4">
<result type="redirectAction">
r2
</result>
</action>
</package>
GlobalResult
package com.demo.action;
import com.opensymphony.xwork2.ActionSupport;
public class AdminAction extends ActionSupport {
@Override
public String execute() throws Exception {
return "mainpage" ;
}
}
package com.demo.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
private int type;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@Override
public String execute() throws Exception {
if(type == 1) {
return
"success" ;
} else if (type == 2) {
return "error" ;
}
return "mainpage" ;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
</head>
<body>
Resutl 类型
<ol>
<li><a href="user/user?type=1">返回success</a></li>
<li><a href="user/user?type=2">返回error</a></li>
<li><a href="user/user?type=3">返回global result</a></li>
<li><a href="admin/admin">admin,继承user包</a></li>
</ol>
</html>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<package name="user" extends="struts-default" namespace="/user">
<global-results>
<result name="mainpage">/main.jsp</result>
</global-results>
<action name="user" class="com.demo.action.UserAction">
<result>/user_success.jsp</result>
<result name="error">/user_error.jsp</result>
</action>
</package>
<package name="admin" namespace="/admin" extends="user">
<action name="admin" class="com.demo.action.AdminAction">
<result>/admin.jsp</result>
</action>
</package>
</struts>
<package name="admin" namespace="/admin" extends="user"> 继承上面package包的内容
共用的actioon
<global-results>
<result name="mainpage">/main.jsp</result>
</global-results>
DynamicResult
package com.demo.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private int type;
private String r;
public String getR() {
return r;
}
public void setR(String r) {
this.r = r;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@Override
public String execute() throws Exception {
if (type == 1) {
r = "/user_success.jsp";
} else if (type == 2) {
r = "/user_error.jsp";
}
return "success";
}
}
<body>
动态结果一定不要往了为动态结果的保存设置set get方法
<ol>
<li><a href="user/user?type=1">返回seuccess</a></li>
<li><a href="user/user?type=2">返回error</a></li>
</ol>
</body>
<package name="user" namespace="/user" extends="struts-default">
<action name="user" class="com.demo.action.UserAction">
<result>${r}</result>
</action>
</package>
User Success !!!
<s:property value="r"/>
<s:debug></s:debug>
package com.demo.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
private int type;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
<package name="user" namespace="/user" extends="struts-default">
<action name="user" class="com.demo.action.UserAction">
<result type="redirect">/user_success.jsp?t=${type}</result>
</action>
</package>
向结果传参数
<ol>
<li>
<a href="user/user?type=1">传参数</a>
</li>
</ol>
<body>
User Success !!<br/>
from valuestack: <s:property value="t"/><br/>
from actioncontext: <s:property value="#prameters.t"/>
<s:debug></s:debug>
</body>

User Success !!
from valuestack:
from actioncontext: 1
[Debug]