现在的位置: 首页 > 综合 > 正文

struts2 ResultType四种基本类型

2017年04月14日 ⁄ 综合 ⁄ 共 3105字 ⁄ 字号 评论关闭

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(实际上就是放到ValueStackCompoundRoot中),所以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

抱歉!评论已关闭.