Struts2_18_GlobalResult

来源:互联网 发布:几米软件 编辑:程序博客网 时间:2024/06/06 05:07
当很多的package包都需要某个result时,将该result设置为globalresult会是明智的选择。但是这个globalResult只能出现在其中一个包里,那么其他包要想使用,就必须继承含有globalResult的包,才能使用globalResult。
摘自网上:
 

struts2的global-result配置  

2010-07-19 11:17:00|  分类: Struts2+Spring+H |字号 订阅

在书写配置文件的时候,一系列冗长的配置让人头痛

可以使用几种方式有效地减少配置的行数

1.通配符配置

2.global-result

3.配置拦截器栈在一个专用的package里,并在此package里设置为default,这样使用的package extends此package即可对package里所有action进行拦截,不用逐一加interceptor-ref标签

 

通配符在上一篇已经讲了

这一篇主要是 全局结果页的跳转

 

很多时候,某些跳转目标页供多个action使用

即在每个action中均要配置一遍

虽然复制粘贴不费力,但是以后的修改配置等非常费神

 

可以在一个package下配置global-reuslt

然后该package下的所有action均不必配置该公共的跳转目标

 

可以使用<global-results>标签来定义全局的<result>,代码如下:

<struts>
<package name="demo" extends="struts-default">
<global-results>
<result name="error">/error.jsp</result>
</global-results>


</package>
</struts>

 

 

如果<action>中没有相应的<result>Struts2就会使用全局的<result>


来源: <http://wukunliang.blog.163.com/blog/static/177665472201061911170408/>

 

 
小例子:
1,新建web-project:Struts2_0500_GlobalResult
2,粘贴jar
3,拷贝web.xml中的filter
4,粘贴struts.xml,并修改
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
  <constant name="struts.devMode" value="true" />
  <constant name="struts.i18n.encoding" value="GBK"></constant>
     <package name="user" namespace="/user" extends="struts-default">
         <global-results>
             <result name="mainpage">/mainPage.jsp</result>
         </global-results>
         <action name="user" class="com.awei.struts2.action.UserAction">
             <result >/user.jsp</result>
             <result name="error">/exception.jsp</result>
         </action>
    </package>
    <package name="admin" namespace="/admin" extends="user" >
        <action name="admin" class="com.awei.struts2.action.AdminAction">
            <result>/admin.jsp</result>
            <result name="error">/exception.jsp</result>
        </action>
    </package>
    <!-- Add packages here -->
</struts>
 
5,com.awei.struts2.action:
package com.awei.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
    private String type;
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String execute(){
        if("1".equals(type)){
            return SUCCESS;
        }else if("2".equals(type)){
            return ERROR;
        }else{
        return "mainpage";
        }
    }
    
}
 
 
 
package com.awei.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
public class AdminAction extends ActionSupport{
    private String type;
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String execute(){
        if("1".equals(type)){
            return SUCCESS;
        }else if("2".equals(type)){
            return ERROR;
        }else{
        return "mainpage";
        }
    }
}
 
6,jsp页面
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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/csshref="styles.css">
    -->
  </head>
  
  <body>
    <a href="user/user?type=1">进入用户界面</a><br/>
    <a href="admin/admin?type=2">进入管理员界面</a>
    <a href="admin/admin?type=4">进入mainPage界面</a>
  </body>
</html>
 
user.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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.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/csshref="styles.css">
    -->
  </head>
  
  <body>
    user,welcome. <br>
  </body>
</html>
 
admin.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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 'admin.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/csshref="styles.css">
    -->
  </head>
  
  <body>
    admin,welcome. <br>
  </body>
</html>
 
mainPage.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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 '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/csshref="styles.css">
    -->
  </head>
  
  <body>
    mainPage. <br>
  </body>
</html>
 
exception.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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 'exception.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/csshref="styles.css">
    -->
  </head>
  
  <body>
    Exception. <br>
  </body>
</html>
0 0
原创粉丝点击