struts2 ResultType四种基本类型
来源:互联网 发布:大智慧下载数据在哪里 编辑:程序博客网 时间:2024/05/16 18:17
ResultType四种基本类型:
<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" class="com.action.Hello3Action"><!--这种配置方法测试不通过,所以是错误的:<result type="chain">/secure/r5</result>--><result type="chain"><param name="actionName">r5</param><param name="namespace">/secure</param></result></action><action name="r4"><result type="redirectAction">r2</result></action></package><package name="secure" namespace="/secure" extends="struts-default"><action name="r5" class="com.action.Hello5Action"><result>/r5.jsp</result></action></package>第一种:dispatcher:服务器端视图跳转,只能跳转到视图,不能跳转到action,实验结果也是这样的
第二种:redirect:客户端重定向到视图
第三种:chain:服务器跳转到action
第四种:redirectAction:客户端重定向到Action,本质上就是客户端发送第二次请求
重点研究第三种跳转,这种方式我觉得有点模糊:
官方示例:
<package name="public" extends="struts-default"> <!-- Chain creatAccount to login, using the default parameter --> <action name="createAccount" class="..."> <result type="chain">login</result> </action> <action name="login" class="..."> <!-- Chain to another namespace --> <result type="chain"> <param name="actionName">dashboard</param> <param name="namespace">/secure</param> </result> </action></package><package name="secure" extends="struts-default" namespace="/secure"> <action name="dashboard" class="..."> <result>dashboard.jsp</result> </action></package>
这是配置方面的用法,一种是同包访问,另一种是不同包访问,示例已经很清楚了,不解释;直接copy
chain这种方式经过多个action跳转以后,它对struts2 的ValueStack有什么影响?这是我们重点讨论的地方,在一次实际应用当中碰到了这个问题
这个问题的理论基础是:在Struts2中,一个请求在最终到达Action的方法之前,Action对象本身会被压入ValueStack(实际上就是放到ValueStack的CompoundRoot中),所以Action对象是CompoundRoot中的一个元素。这是我看了李老师的博客才最终解决这个问题的,有兴趣可以参见李老师的原文,对ongl以及struts2解释相当的深刻:
http://blog.csdn.net/li_tengfei/article/details/6098134
根据上面的struts配置文件,这是chain方式跳转的两个action:
public class Hello3Action extends ActionSupport {private String r3 = null;private String name = null;public String execute() {r3 = "Hello3Action r3";name = "Helle3Action name";System.out.println();return SUCCESS;}public String getR3() {return r3;}public void setR3(String r3) {this.r3 = r3;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
Hello5Action:
package com.action;import com.opensymphony.xwork2.ActionSupport;public class Hello5Action extends ActionSupport{private String r5 = null;private String name = null;public String execute(){r5 = "Hello5Action r5";name = "Helle5Action name";return SUCCESS;}public String getR5() {return r5;}public void setR5(String r5) {this.r5 = r5;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
根据struts2的配置文件,首先请求r3 action,然后r3 action服务器跳转到r5 action,然后服务器跳转到r5.jsp,展现结果:
根据李老师的理论,那么Hello3Action会先压入ValueStack对象,然后Hello5Action再压入ValueStack,如果要取想Hello3Action的name属性,可以用:<s:property value="#root[1].name"/> 取出Hello3Action的name属性
下面是取值代码:
r5页面<br/>直接访问r3:${r3}<br/>直接访问r5:${r5}<br/>访问r3对象:${root[1].r3 }<br/>访问r5对象:${root[0].r5 }<br/><hr/>直接访问name属性,必只能访问到Hello5Action的name属性:${name }<br/>必须使用另外的方式访问r3对象了:${root[1].name }<br /><s:property value="#root[1].name" />
相关ValueStack的机制:http://blog.csdn.net/li_tengfei/article/details/6098134
ValueStack和CompoundRoot的关系:http://hi.baidu.com/maml507/item/d2051132965b6e109cc65ee3- struts2 ResultType四种基本类型
- struts2的resultType类型
- Struts2 resultType返回类型
- struts2 07 结果类型 ResultType
- Struts2-ResultType-1-结果类型
- struts2-resultType
- struts2系列十三之resulttype的四种类型
- resultype四种之一默认的resultType
- Struts2源码浅析-ResultType
- struts2 更改默认resulttype
- struts2学习笔记----resultType
- struts2的resultType
- 【struts2】Result和ResultType
- struts2之resultType
- chain类型的ResultType
- 图像 的 四种基本类型
- Zigbee | 四种基本的frame类型
- C++四种基本类型转换
- 检查异常和未检查异常不同之处
- java 实现文件分割
- sd卡插拔广播
- Ubuntu 12.04 安装后的配置
- java 输出某文件夹下的所有文件名称
- struts2 ResultType四种基本类型
- 复制文件
- linux make使用的简单翻译
- IT Skill: How to Recall Email Message in Outlook 2007
- 刷新SQL SERVER所有视图、函数、存储过程
- POI 操作Excel常用方法
- c++ to asm
- 数据库基础笔记1
- Android 的系统属性(SystemProperties)设置分析