Result类型
Struts2.xml代码:
还有一个:
then UserAction.java:
and then struts.xml:
last user_success.jsp:
- dispatcher (默认的)
- redirect
- chain
- redirectAction
- freemarker
- httpheader
- stream
- velocity
- xslt
- plaintext
- tiles
<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>
再上struts.xml:
<struts>
<constant name="struts.devMode" value="true" />
<package name="resultTypes" namespace="/r" extends="struts-default">
<action name="r1">
<result type="dispatcher">/r1.jsp</result><!-- 服务器端跳转dispatcher只能到视图,不能到Action,就是servlet -->
</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>
</struts>
GlobalResult-
index.htnl代码:
<body>
Result类型
<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>
</body>
Struts2.xml代码:
<struts>
<constant name="struts.devMode" value="true" />
<package name="user" namespace="/user" extends="struts-default">
<global-results>
<result name="mainpage">/main.jsp</result>
</global-results>
<action name="index">
<result>/index.jsp</result>
</action>
<action name="user" class="com.china.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.china.AdminAction">
<result>/admin.jsp</result>
</action>
</package>
</struts>
几个用到的java程序:
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";
else return "mainpage";
}
}
还有一个:
import com.opensymphony.xwork2.ActionSupport;
public class AdminAction extends ActionSupport {
@Override
public String execute() throws Exception {
return "mainpage";
}
}
DynamicResult动态结果集:(了解的级别)
first index.jsp:
<body>
动态结果一定不要忘了为动态结果的保存值设置set get方法
<ol>
<li><a href="user/user?type=1">返回success</a></li>
<li><a href="user/user?type=2">返回error</a></li>
</ol>
</body>
then UserAction.java:
mport 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";
}
}
last struts2.xml:
<struts>
<constant name="struts.devMode" value="true" />
<package name="user" namespace="/user" extends="struts-default">
<action name="user" class="com.china.UserAction">
<result>${r}</result>
</action>
</package>
</struts>
ResultWithParams:
frist index.jsp:
<body>
向结果传参数
<ol>
<li><a href="user/user?type=1">传参数</a></li>
</ol>
</body>
then UserAction.java:
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";
}
}
and then struts.xml:
<struts>
<constant name="struts.devMode" value="true" />
<package name="user" namespace="/user" extends="struts-default">
<action name="user" class="com.china.UserAction">
<result type="redirect">/user_success.jsp?t=${type}</result>
</action>
</package>
</struts>
last user_success.jsp:
<body>
User Success!</br>
from valuestack: <s:property value="t"/><br/><!--this is wrong,can't receiver-->
from actioncontext: <s:property value="#parameters.t"/>
<s:debug></s:debug>
</body>