主要内容:
- 1.动态结果集(了解)
- 2.带参数结果集
一、动态结果集
这块内容用的不多,只需要了解即可。
相关代码:(工程Struts2_1700_DynamicResult
)
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<package name="user" namespace="/user" extends="struts-default">
<action name="user" class="com.bjsxt.struts2.user.action.UserAction">
<result>${target}.jsp</result>
</action>
</package>
</struts>
UserAction
package com.bjsxt.struts2.user.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private int type;
private String target;
@Override
public String execute() throws Exception {
if(type == 1){
target = "/user_success";
}else if(type == 2){
target = "/user_error";
}
return "success";
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
}
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
动态结果(不要忘记为动态结果的保存值设置getter和setter方法)
<ol>
<li><a href="user/user?type=1">返回success</a></li>
<li><a href="user/user?type=2">返回error</a></li>
</ol>
</body>
</html>
user_success.jsp
<body>
User Success
<s:property value="target" />
<s:debug></s:debug>
</body>
user_error.jsp
<body>
User Error
</body>
说明:我们知道对于action的属性值一般都存在value stack中,所以我们可以将结果也当成一个属性存入value stack中。这里有个问题是,本来我们配置result应该是这样配置的:
<result>${target}</result>
然后在action中target的值应该是jsp页面的全名,但是我这样配置之后发现报错,于是在网上搜索了一下,发现上面这种配置能够解决问题,不清楚是什么问题。
二、带参数的结果集
我们知道当浏览器一次请求时服务器中是共享参数的,就比如forward跳转,但是对于redirect跳转来说,参数不是共享的,因为浏览器向服务器发起了多次请求。于是我们在跳转时有时需要带上相关参数。
相关代码:(工程Struts2_1800_ResultWithParams
)
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<package name="user" namespace="/user" extends="struts-default">
<action name="user" class="com.bjsxt.struts2.user.action.UserAction">
<result type="redirect">/user_success.jsp?target=${type}</result>
</action>
</package>
</struts>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
向结果传递参数
<ol>
<li><a href="user/user?type=1">传递参数</a></li>
</ol>
</body>
</html>
UserAction.java
package com.bjsxt.struts2.user.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private int type;
@Override
public String execute() throws Exception {
return "success";
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
user_success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>User Success</title>
</head>
<body>
User Success<br>
from value stack :<s:property value="target" /><br>
from action context: <s:property value="#parameters.target" />
<s:debug></s:debug>
</body>
</html>
说明:
- 1.当我这样配置时
<result type="redirect">/user_success.jsp?target=${type}</result>
,发现报错,最后通过配置MyEclipse解决了:在Window->prefrences->MyEclipse->Validation->Struts2 Validator->去掉build的勾
重启就可以了。但是这种方式没有解决上个例子的问题。 - 2.当我们使用
<s:property value="target" />
发现是取不到值的,这是因为target本来就不是action中的参数,但是我们可以在action context中取得这个值。
最后:到此,result的配置就基本讲完了。