---
title: "struts2的配置文件global-results注意事项"
author: "lzstone"
date: "2010-04-10T11:09:20Z"
tags: []
categories: ["企业架构"]
slug: "struts2-global-results"
draft: false
lastmod: "2010-04-11T11:49:28Z"
id: 640454
url: "https://lzstone.iteye.com/blog/640454"
---

struts.xml

&lt;struts&gt;

    &lt;include file="GoogleSuggest.xml" /&gt;  
    &lt;include file="login.xml"&gt;&lt;/include&gt;  
    &lt;include file="book.xml"&gt;&lt;/include&gt;  
    &lt;include file="upload.xml"&gt;&lt;/include&gt;

    &lt;package name="struts2" namespace="/" extends="struts-default"&gt;  
        &lt;global-results&gt;  
            &lt;result name="index"&gt;/index.jsp&lt;/result&gt;  
        &lt;/global-results&gt;  
         
        &lt;default-action-ref name="index" /&gt;  
         
        &lt;action name="index" class="org.lzstone.index.action.IndexAction"&gt;  
         
            &lt;result name="success"&gt;/index.jsp&lt;/result&gt;  
        &lt;/action&gt;

    &lt;/package&gt;

&lt;/struts&gt;

IDE报错，发现将&lt;global-results&gt;放置到&lt;default-action-ref name="index" /&gt;之下错误消失，发现default的优先级别还是很高的。

写了一个简单拦截器

public class LoginInterceptor implements Interceptor{  
   

    @Override  
    public void destroy() {  
         
        System.out.println("最终离开Interceptor");  
    }

    @Override  
    public void init() {  
         
        System.out.println("开始进入Interceptor");  
         
    }

    @Override  
    public String intercept(ActionInvocation invocation) throws Exception {  
         
        ActionContext act = invocation.getInvocationContext();  
        Map session = act.getSession();  
        Object object = session.get("user");  
        if(null != object){  
            return invocation.invoke();  
        }  
        return "index";  
    }

     
     
}

失败返回index，拦截器注册在

&lt;struts&gt;

    &lt;package name="login" namespace="/" extends="struts-default"&gt;

        &lt;interceptors&gt;  
            &lt;interceptor name="loginInterceptor"  
                class="org.lzstone.interceptor.LoginInterceptor" /&gt;  
        &lt;/interceptors&gt;  
         
        &lt;action name="login" class="org.lzstone.login.action.LoginAction"&gt;  
            &lt;result name="success"&gt;/success.jsp&lt;/result&gt;  
            &lt;result name="error"&gt;/error.jsp&lt;/result&gt;  
            &lt;interceptor-ref name="defaultStack" /&gt;  
            &lt;interceptor-ref name="loginInterceptor" /&gt;  
        &lt;/action&gt;  
    &lt;/package&gt;

&lt;/struts&gt;

测试的时候发现无法发现index，global-results下面我已经有index

后再在login.xml下面加入

&lt;global-results&gt;  
            &lt;result name="index"&gt;/index.jsp&lt;/result&gt;  
        &lt;/global-results&gt;

运行成功

结果发现struts2中的package限制还是很大的，global-results也只是存在一个包的全局