Struts2中的全局结果集

1.在Struts2中,当有多个Action使用同一个结果集时,则可以使用全局结果集(GlobalResult),这样就不用在每一个使用同一个结果集的action里面都添加一个result,可以减少代码量,优化struts.xml配置文件,接下来将用一个很简单的小例子来介绍全局结果集。



2.首先,新建一个struts2项目,打开index.jsp页面,编码格式改为utf-8,在对里面的代码进行修改,修改后代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
	<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 结果集</a></li>
	<li><a href="other/other">other,继承user包</a></li>
	</ol>
  </body>
</html>


对应的struts.xml配置文件如下:

<package name="user" namespace="/user" extends="struts-default">
	<global-results>
		<result name="mainpage">/user_mainpage.jsp</result>
	</global-results>
		
	<action name="user" class="com.gk.UserAction">
		<result name="success">/user_success.jsp</result>
		<result name="error">/user_error.jsp</result>
	</action>
</package>


action所对应的Action类,UserAction类,代码如下:

package com.gk;

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;
	}

	public String execute() {
		if (type == 1) {
			return "success";
		} else if (type == 2) {
			return "error";
		} else {
			return "mainpage";
		}
	}
}

其中UserAction类中返回的结果集有"mainpage",而在struts.xml配置文件中的user的action中并没有"mainpage"的result,在这时就会使用全局的结果集(global-result),这个结果集中有"mainpage"的result。



3.当其它不同的package需要使用这个全局的result时,则需要使用<package>这个标签中的extends属性来指定包含全局的package就可以了。

例如,有个OtherAction.java文件,代码如下:

package com.gk;

import com.opensymphony.xwork2.ActionSupport;

public class OtherAction extends ActionSupport{
	
	public String execute(){
		return "mainpage";
		
	}
}


对应的struts.xml配置文件中的配置如下:

<package name="other" namespace="/other" extends="user">
	<action name="other" class="com.gk.OtherAction">
		<result>/other.jsp</result>
	</action>
</package>


这时user这个package中的结果集就可以被other这个package中的action所使用了,并不会跳转到other.jsp页面去,而是跳转到全局结果集中的user_mainpage.jsp页面上去了。



4.接下来附上上面没有的代码,其中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.enable.DynamicMethodInvocation" value="true"></constant>
	<constant name="struts.devMode" value="true"></constant>
	<package name="user" namespace="/user" extends="struts-default">
		<global-results>
			<result name="mainpage">/user_mainpage.jsp</result>
		</global-results>
		
		<action name="user" class="com.gk.UserAction">
			<result name="success">/user_success.jsp</result>
			<result name="error">/user_error.jsp</result>
		</action>
	</package>
	
	<package name="other" namespace="/other" extends="user">
		<action name="other" class="com.gk.OtherAction">
			<result>/other.jsp</result>
		</action>
	</package>
</struts>

user_success.jsp页面代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'user_success.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    返回成功
  </body>
</html>


user_error.jsp页面代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'user_error.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    返回错误
  </body>
</html>

user_mainpage.jsp页面代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'user_mainpage.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    返回主页面
  </body>
</html>


other.jsp页面代码如下,根据配置文件,是不会跳转到这个页面的:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'other.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    其他页面
  </body>
</html>



4.这个简单的小项目结构,如下图所示:

Struts2中的全局结果集_第1张图片


部署该项目到Tomcat服务器上,开启Tomcat服务器,运行后如下图所示:


分别点击上图中4个超链接,依次效果如下:

Struts2中的全局结果集_第2张图片


Struts2中的全局结果集_第3张图片


Struts2中的全局结果集_第4张图片

在本包内,返回全局结果集。

Struts2中的全局结果集_第5张图片

在其它包里,继承user包,返回全局结果集。



5.以上内容仅供大家学习参考,谢谢!

你可能感兴趣的:(struts2,全局结果集,GlobalResult)