J2EE系列之Struts2学习笔记(十一)--result配置

前面讲过了package配置以及action配置,现在来说一下result配置。result标签常用的属性有name和type两种,其中name代表处理类返回的结果值,这个前面已经用过多次了。type属性前面一直没有用过。type属性的取值最常用的有dispatcher、redirect、chain以及redirectAction四种。type默认取值为dispatcher,前面我们一直使用的就是这个默认取值属性。

首先来粗略说一下各个取值的作用:

dispatcher:向要跳转的网页转发数据,跳转到的网页能够取到Action类中的变量值。这个我们之前的示例中也看到了。

redirect:重定向到要跳转的网页,跳转到的网页不能取到Action类中的变量的值。

chain:转发到另一个请求,这里把第一个Action中的参数值也一起传递。

redirectAction:重定向到另一个请求,这里第一个Action中的变量值不能传递。


有的时候有很多action会共用一个<result>,这时就可以把这个<result>定义成全局的。


通过示例来看一下:

1.新建一个工程:SecondLearnStruts2Chap02_07;

2.新建两个Action处理类:HelloAction和HelloAction2;

HelloAction:

package com.test.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String name;
	private String error;
	
	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	@Override
	public String execute() throws Exception {
		if(name==null || "".equals(name)){
			this.error="name是空";
			return ERROR;
		}
		return SUCCESS;
	}
	
	
	public String r()throws Exception{
		return "r";
	}
	
	public String c()throws Exception{
		return "c";
	}

	public String ra()throws Exception{
		return "ra";
	}
	
	
	
}
这里定义了两个变量name和error,并写了几个处理函数。默认执行函数中,如果name变量为空,则给error变量赋值,并返回error。


HelloAction2:

package com.test.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction2 extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String name2;
	
	



	public String getName2() {
		return name2;
	}


	public void setName2(String name2) {
		this.name2 = name2;
	}


	@Override
	public String execute() throws Exception {
		this.name2="你好啊";
		return SUCCESS;
	}
	
	
}

这里定义了一个变量,并在默认执行函数中对这个变量进行了赋值。


struts.xml配置文件为:

<struts>
	
  <constant name="struts.enable.DynamicMethodInvocation" value="true" />
  
  <package name="manage" namespace="/" extends="struts-default">
  	<global-results>
  		<result name="error">error.jsp</result>
  	</global-results>
  	
  	<action name="hello" class="com.test.action.HelloAction">
  		<result name="success" type="dispatcher">success.jsp</result>
  		<result name="r" type="redirect">success.jsp</result>
  		<result name="c" type="chain">hello2</result>
  		<result name="ra" type="redirectAction">hello2</result>
  	</action>
  	
  	<action name="hello2" class="com.test.action.HelloAction2">
  		<result name="success" type="dispatcher">success.jsp</result>
  	</action>
  </package>

</struts>

这里允许使用动态调用方法。使用<global-results>标签把名为error的result定义成了全局的。上面HelloAction的默认执行方法中当name变量为空时,会返回error值。在名为hello的action中并没有定义名为error的result,此时在名为hello的action中找不到这个result后,Struts2会到全局的result中去找。像这个例子中,当HelloAction返回error时,就会找到全局的result,并跳转到error.jsp页面。

hello这个action中,我们分别使用了type的四个不同取值:第一个使用dispatcher转发到success.jsp页面,第二个重定向到success.jsp页面;第三个转发到hello2这个请求,第四个重定向到hello2这个请求。

hello2这个action中,当返回success时会转发到success这个页面。


新建success.jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	${name }</br>
	${name2 }
</body>
</html>

我们在这里分别取HelloAction中的name变量的值和HelloAction2中name2变量的值。


新建error.jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
错误信息:${error }
</body>
</html>

这里取HelloAction中error变量的值。


新建index.jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="hello?name=Struts2" target="_blank">默认转发_dispatcher</a><br/>
<a href="hello!r?name=Struts2" target="_blank">重定向_redirect</a><br/>
<a href="hello!c?name=Struts2" target="_blank">链条_chain</a><br/>
<a href="hello!ra?name=Struts2" target="_blank">重定向到Action_redirectAction</a><br/>

<a href="hello" target="_blank">全局result配置</a><br/>
</body>
</html>


运行程序:


点击默认转发dispatcher:


这里我们给HelloAction中的name变量传值为Struts2,执行execute函数后返回success,从而转发到success.jsp页面,在success.jsp页面上能够取到HelloAction中name变量的值。


点击重定向redirect:


这里我们给HelloAction中的name变量传值为Struts2,动态调用的方式执行r函数后返回r,从而重定向到success.jsp页面,在success.jsp页面上没有能够取到HelloAction中name变量的值。


点击链条chain:



这里我们给HelloAction中的name变量传值为Struts2,动态调用的方式执行c函数后返回c,从而转发到到执行请求hello2,此时HelloAction中name变量的值也传递了过来,在HelloAction2的execute函数中给name2变量赋值你好啊之后返回success,进而转发到success.jsp页面,在success.jsp页面上同时取到了HelloAction中name变量的值以及HelloAction2中name2变量的值。


点击重定向到Action_redirectAction:


这里我们给HelloAction中的name变量传值为Struts2,动态调用的方式执行ra函数后返回ra,从而重定向到执行请求hello2,此时HelloAction中的name变量的值不能传递到HelloAction2中,在HelloAction2的execute函数中给name2变量赋值你好啊之后返回success,进而转发到success.jsp页面,在success.jsp页面上之取到了HelloAction2中name2变量的值。


点击全局result配置:


这里我们给HelloAction中的name没有赋值,执行execute函数后返回error,在全局result中找到了名为error的result,接下来转发到error.jsp页面,在该页面上显示HelloAction中error变量的值。





  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值