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 ...
随机推荐
- C++中##(两个井号)和#(一个井号)用法
C(和C++)中的宏(Macro)属于编译器预处理的范畴,属于编译期概念(而非运行期概念).下面对常遇到的宏的使用问题做了简单总结.关 于#和##在C语言的宏中,#的功能是将其后面的宏参数进行字符串化 ...
- CSS3的box-sizing属性
盒模型的宽度,在 IE5.x 以及 Quirks 模式的 IE6/7 中,将 border 与 padding 都包含在 width 之内 W3C标准中的盒模型宽度为内容宽度,不包括内边距paddin ...
- CodeFroces--Good Bye 2016-B--New Year and North Pole(水题-模拟)
B. New Year and North Pole time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- zen cart global $db 这噶哒
zen cart $db变量是mysql数据库类的对象,是一个全局变量.在zen cart系统里面的任何一个地方都可以使用这个对象来查询数据库获取数据库的内容.只要需要用到查询数据库,都可以使用这个对 ...
- akka 入门
http://blog.csdn.net/thkhxm/article/details/40182835 1.首先安装akka的相关包-- http://akka.io/downloads/2.导入依 ...
- Perl资料
一 官网 http://www.perl.org/ 三 资料 http://www.slideshare.net/ggilmour/perl-development-sample-courseware ...
- jenkins配置角色访问
本文将介绍如何配置jenkins,使其可以支持基于角色的项目权限管理. 由于jenkins默认的权限管理体系不支持用户组或角色的配置,因此需要安装第三发插件来支持角色的配置,本文将使用Role Str ...
- Androidndk开发打包时我们应该如何注意平台的兼容(x86,arm,arm-v7a)
很多朋友在开发Android JNI的的时候,会遇到findlibrary returned null的错误,因为某种原因,so没有打包到apk中.下面浅析下引起该错误的原因以及平台兼容性问题. 一. ...
- 自己开发的轻量级gif动画录制工具
虽然网上已经有LICEcap.GifCam等gif录制工具,但我仍然觉得对于我个人使用还是不够方面,所以自己又写了一个,功能相对简洁一些. Gif Recorder 支持全屏录制和区域录制,可自 ...
- CodeForces 709B Checkpoints 模拟
题目大意:给出n个点的坐标,和你当前的坐标,求走过n-1个点的最短路程. 题目思路:走过n-1个点,为了使路程更短,那么不走的点只可能第一个点或最后一个点.模拟就行了,比较恶心. #include&l ...