J2EE系列之Struts2学习笔记(十一)--result配置
2017-04-21 17:34
465 查看
前面讲过了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变量的值不能传递到H
b640
elloAction2中,在HelloAction2的execute函数中给name2变量赋值你好啊之后返回success,进而转发到success.jsp页面,在success.jsp页面上之取到了HelloAction2中name2变量的值。
点击全局result配置:
这里我们给HelloAction中的name没有赋值,执行execute函数后返回error,在全局result中找到了名为error的result,接下来转发到error.jsp页面,在该页面上显示HelloAction中error变量的值。
首先来粗略说一下各个取值的作用:
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变量的值不能传递到H
b640
elloAction2中,在HelloAction2的execute函数中给name2变量赋值你好啊之后返回success,进而转发到success.jsp页面,在success.jsp页面上之取到了HelloAction2中name2变量的值。
点击全局result配置:
这里我们给HelloAction中的name没有赋值,执行execute函数后返回error,在全局result中找到了名为error的result,接下来转发到error.jsp页面,在该页面上显示HelloAction中error变量的值。
相关文章推荐
- J2EE系列之Struts2学习笔记(八)--struts.xml分模块配置
- J2EE系列之MyBatis学习笔记(十一)-- 使用注解配置sql映射器
- J2EE系列之Struts2学习笔记(七)--struts.xml配置
- J2EE系列之Struts2学习笔记(九)--使用通配符配置struts.xml文件
- J2EE系列之Struts2学习笔记(一)--Struts2安装与配置
- MongoDB副本集配置系列十一:MongoDB 数据同步原理和自动故障转移的原理
- 【Struts2学习笔记(2)】Action默认值和配置Action于result各种转发类型
- SCCM 2012 SP1系列(十一)配置Endpoint Protection-1
- SCCM2012系列之十一,SCCM2012软件更新部署配置
- Oracle Golden Gate 系列十一 -- 配置 GG DDL 同步 说明 与 示例
- 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap
- 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap
- Oracle Golden Gate 系列十一 -- 配置 GG DDL 同步 说明 与 示例
- 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap
- 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap
- Struts2学习笔记(三):result配置的各项视图转发类型
- Exchange2003-2010迁移系列之十一,Exchange2010 OWA配置
- Oracle Golden Gate 系列十一 -- 配置 GG DDL 同步 说明 与 示例
- Struts2学习笔记(3)——Result的4中类型配置
- Struts2学习笔记(9)-Result配置全局结果集