Struts2 语法--result type
- result type:
dispatcher,redirect:只能跳转到jsp,html之类的页面,dispatcher属于服务器跳转, redirect属于客户端跳转
chain: 等同于forwardaction,
redirectAction: 客户端跳转到另一个action
还有freemarker,httpheader,stream(下载),volocity(类似freemarker), xslt, plaintext,tiles
<?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="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>
</struts>
对于以下dispatcher:
URL: r/r1
显示:r1
<action name="r1">
<result type="dispatcher">/r1.jsp</result>
</action>
对于以下redirect:
URL: r/r2.jsp
显示:r2
<action name="r1">
<result type="redirect">/r2.jsp</result>
</action>
对于以下chain:
URL: r/r3
显示:r1
<action name="r1">
<result type="chain">r1</result>
</action>
注意: 如果forward到别的package的action的话, 需要用这个命令:\
<result type="chain"> <param name="actionname">dashboard</param> <param name="namespace">/secure</param> </result>
对于以下redirectAction:
URL: r/r2.jsp
显示:r2
<action name="r1">
<result type="redirectAction">r2</result>
</action>
- 全局结果集:
jsp:
<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>
useraction:
@Override
public String execute() throws Exception {
if(type == 1) return "success";
else if (type == 2) return "error";
else return "mainpage";
}
如果很多action都有一个共同的结果页面, 比如上述的mainpage, 就可以单独提出来作为全局结果集:
如下struts.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.bjsxt.struts2.user.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.bjsxt.struts2.user.action.AdminAction">
<result>/admin.jsp</result>
</action>
</package>
</struts>
如果admin的package要想使用全局结果局, 只需要继承user包即可:
extends="user"
- 动态结果集:dynamic result:
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>
userAction:
package com.bjsxt.struts2.user.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";
}
}
struts.xml:
<?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" namespace="/user" extends="struts-default"> <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
<result>${r}</result>
</action>
</package>
</struts>
${r}用于从值栈 value stack里取值,每个action都有一个值栈
r在userAction里是动态确定的.所以叫做动态调用
此符号是xml配置文件里的Ognl表达式, 专门用于从action值栈里取出内容的.
- 带参数的结果集:
jsp:
<body>
向结果传参数
<ol>
<li><a href="user/user?type=1">传参数</a></li>
</ol>
</body>
userAction:
package com.bjsxt.struts2.user.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";
}
}
struts.xml:
<?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" namespace="/user" extends="struts-default"> <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
<result type="redirect">/user_success.jsp?t=${type}</result>
</action>
</package>
</struts>
user_success.jsp:
因为是redirect客户端跳转, 所以不能共享一个值栈,即不能使用type, 所以要用t=${type}
也可以说一次request只有一个值栈,
<body>
User Success!
from valuestack: <s:property value="t"/><br/> //取不到
from actioncontext: <s:property value="#parameters.t"/>
<s:debug></s:debug>
</body>
<s:property value="t"/>格式属于从值栈取值, 因为是第二次request,当前没有action,所以没有值栈,也不能共用之前的值栈的值,
所以应该用<s:property value="#parameters.t"/>来取actionContext的值
Struts2 语法--result type的更多相关文章
- Struts2 中result type属性说明
Struts2 中result type属性说明 首先看一下在struts-default.xml中对于result-type的定义: <result-types><result-t ...
- struts2 action result type类型
struts2 action result type类型 1.chain:用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息. com.opensymphony. ...
- struts2 中 result type="stream"
Stream result type是Struts2中比较有用的一个feature.特别是在动态生成图片和文档下载的情况下 1:图片验证码: Action类,action主要要提供一个获取InputS ...
- Struts2 中 result type=”json” 的参数解释
转自:http://wangquanhpu.iteye.com/blog/1461750 1, ignoreHierarchy 参数:表示是否忽略等级,也就是继承关系,比如:TestAction 继承 ...
- struts2文件下载 <result type="stream">
<!--struts.xml配置--> <action name="download" class="com.unmi.action.DownloadA ...
- jquery序列化from表单使用ajax提交返回json数据(使用struts2注解result type = json)
1.action类引入struts2的"json-default"拦截器栈 @ParentPackage("json-default") //示例 @Paren ...
- Struts2配置RESULT中TYPE的参数说明
chain 用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息. com.opensymphony.xwork2.Acti ...
- struts2.xml 中result type属性说明
chain 用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息. com.opensymphony.xwork2.Acti ...
- Struts2 配置文件result的name属性和type属性
Struts2 配置文件result的name属性和type属性:Name属性SUCCESS:Action正确的执行完成,返回相应的视图,success是 name属性的默认值: NONE:表示Act ...
随机推荐
- wcf中的使用全双工通信
wcf中的契约通信默认是请求恢复的方式,当客户端发出请求后,一直到服务端回复时,才可以继续执行下面的代码. 除了使用请求应答方式的通信外,还可以使用全双工.下面给出例子: 1.添加一个wcf类库 2. ...
- Objective-C 2.0属性(Property)介绍
通常在声明一些成员变量时会看到如下声明方式: @property (参数1,参数2) 类型 名字: 这里我们主要分析在括号中放入的参数,主要有以下三种: setter/getter方法(assign/ ...
- android开发进阶学习博客资源
Android开发者博客推荐 Android入门级 - 罗宪明 http://blog.csdn.net/wdaming1986 Android入门级 - 魏祝林 http://blog.csdn.n ...
- HDU 5795 A Simple Nim
打表找SG函数规律. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> ...
- java方法中只有值传递,没有引用传递
public class Example { String testString = new String("good"); char[] testCharArray = {'a' ...
- 第3章 Java语言基础----static
1.static只能声明成员变量,不能声明局部变量,如下图所示: 2.如果变量在类中用static中定义过,那么在方法中就可以直接赋值了:如果没有在类中定义,则不能在方法中使用,还得重新定义,如下图所 ...
- <c:if>替代
由于没有else, 由下面的替代 <c:choose> <c:when test="${usersession.hasPrivilegeByName('Case Delet ...
- PHP数组函数试题
使用Ctrl+A查看答案 1.将数组的键名全部转换成小写和大写的函数是什么?答:array_change_key_case($array [,CASE_LOWER|CASE_UPPER]) 2.创建一 ...
- JSP精华知识点总结
本文转自:http://blog.csdn.net/qy1387/article/details/8050239 JSP精华知识点总结 Servlet三个要素 1.必须继承自HttpServlet 2 ...
- 网址组成与特殊ip小解
网址 https://www.baidu.com:8010/a/html/a.html?tn=monline_3_dg#part1 注解: 网址= 当前url协议+域名+端口号+路径名+参数+hash ...