结果类型
result的type类型:
- dispatcher
- redirect
- chain
- redirectAction
struts.xml:
<package name="resultTypes" namespace="/r" extends="struts-default">
<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>
我写了4个Action,每一个Action都对应了一个结果类型
第一个Action是默认的跳转结果类型,会直接在请求
服务器再跳转,其访问地址不变,为
http://localhost:8080/r/r1
第二个Action是
客户端之间的跳转,其地址将变为:
http://localhost:8080/r2.jsp
第三个Action的结果类型访问的是r1,而r1是跳转至r1.jsp。所以其地址为:
http://localhost:8080/r/r1
http://localhost:8080/r/r1
第四个Action的结果类型访问的是r2,而r2是跳转至r2.jsp,其结果地址为:
http://localhost:8080/r2.jsp
http://localhost:8080/r2.jsp
注意:使用chain时,若是需要使用另外一个包的action,那么就需要在result标签内写上param标签
如:
<action name="r3">
<result type="chain">
<param name="actionName">actionName</param>
<param name="/namespace">namespace</param>
</result>
</action>
全局结果集
定义一个可以共用的结果集,配置如下: <package name="user" namespace="/user" extends="struts-default">
<global-results>
<result name="mainpage">/main.jsp</result>
</global-results>
<action name="user" class="action.userAction">
<result> /user_success.jsp </result>
<result name="error">/user_error.jsp</result>
</action>
</package>
Action代码:
package action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.inject.Scope;
/**
* Created by Alex on 2017/5/15.
*/
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";
}
}
那么只要是返回值为mainpage时,都会跳转至:main.jsp页面
如果其他的包要用这个全局结果集,那么就需要用到extends,代码如下:
<package name="admin" namespace="/admin" extends="user">
<action name="admin" class="action.adminAction">
<result>/admin.jsp</result>
</action>
</package>
Action代码如下:
package action;
import com.opensymphony.xwork2.ActionSupport;
/**
* Created by Alex on 2017/5/15.
*/
public class adminAction extends ActionSupport {
@Override
public String execute(){
return "mainpage";
}
}
这样配置,使得我们新包继承了原来的包的全局结果集,所以可以使用该结果集。
动态结果集
使用特殊标志来动态接受结果集
struts配置如下:
<constant name="struts.devMode" value="true" />
<package name="user" namespace="/user" extends="struts-default">
<action name="user" class="action.userAction">
<result>${r}</result>
</action>
</package>
可以看到我们的result标签内跳转的页面名改为了${r}的形式,为何使用这种结构呢?
请看Action的代码:
package action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.inject.Scope;
/**
* Created by Alex on 2017/5/15.
*/
public class userAction extends ActionSupport {
private String r;
private int type;
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;
}
}
可以看到我们action代码里,定义了两个变量,生成了2个get and set方法,他们分别为type和r
type就是我们传进来的类型,r就是我们在action内定义的跳转页面名
可是为何我们能在struts.xml内接收到这个r的值呢?
原来struts访问了valueStack,值栈,我们在action内将r存进值栈了,在struts那边就可以用${r}来取到。
可以看到我们的r值就在这里面。
p.s 这种表达式名称为 OGNL表达式
带参数的结果集
使用ONGL表达式传参数,struts配置文件如下:
<package name="user" namespace="/user" extends="struts-default">
<action name="user" class="action.userAction">
<result type="redirect"> /user_success.jsp?t=${type}</result>
</action>
</package>
可以看到在result内,我们传过来的参数使用了ONGL表达式来接收
接受jsp页面代码:
<%--
Created by IntelliJ IDEA.
User: Alex
Date: 2017/5/15
Time: 22:05
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>user success</title>
</head>
<body>
user success!! <br/>
from valueStack:<s:property value="r" /><br/>
from actionContext: <s:property value="#parameters.t"/>
<s:debug></s:debug>
</body>
</html>
效果如图
具体关系图:
可以看到传过来的参数我们并没有储存至值栈内,而是直接传到页面了,所以我们用
<s:property value="t" />
并不能取到值,而用
<s:property value="#parameters.t"/>
就能从actionContext里将t值取出
关系如下图:
在value stack内并没有t的值
但是在stack context内就能找到t的值
结果集(result)总结
Result:
- 常用的四中类型:
- dispatcher(默认)
- redirect
- chain
- redirecAction
- 全局结果集
- global-results || extends(继承另外一个包的结果集)
- 动态结果(了解即可)
- 在action中保存一个属性,储存具体的结果location
- 传递参数
- 客户端跳转才需要传递
- ${}(ONGL表达式 不是EL表达式) 从valueStack取值