2010-11-16 22 views
1

所以我有一個我正在開發的Struts2應用程序。在我的首頁上,我有一個部分將顯示我的應用程序拋出的任何異常。這工作得很好,直到我添加了一個自定義攔截器。Struts2應用程序添加攔截器後隱藏我的例外

這裏是我的代碼攔截器:

public String intercept(ActionInvocation actionInvocation) throws Exception { 

    String result = actionInvocation.invoke(); 

    return result; 
} 

這是被生成的異常在我的Action類的代碼,它發生在那裏AuthService.Authorize()被調用:

if(AuthService.Authorize(username, password)) { 
     if(AuthService.AdminAuthorized()) { 
      return "admin"; 
     } 
     return SUCCESS; 
    } 

這在AuthService.Authorize()中時,它在訪問acc時拋出空點異常:

try { 
      acc = profileRepository.WhereSingle("Username", "=", username); 
     } catch (Exception e) { 
      return false; 
     } 

     if (acc.Password.equals(password)) { 

但是,頁面加載時。這不是人口:

<s:property value="%{exception.message}"/> 

我測試了它,它會工作,如果我只是從Action類拋出一個異常。我不是調用redirectAction或任何

這裏是我的所有其他包可擴展

<package name="default" namespace="/" extends="struts-default"> 

    <!-- Interceptors --> 
    <interceptors> 
     <interceptor name="conversation" class="global.ConversationInterceptor"/> 
     <interceptor-stack name="dils-stack"> 
      <interceptor-ref name="defaultStack"/> 
      <interceptor-ref name="conversation"/>     
     </interceptor-stack> 
    </interceptors> 

    <default-interceptor-ref name="dils-stack"/> 

    <global-results> 
     <result name="Exception" >/index.jsp</result> 
    </global-results> 

    <global-exception-mappings> 
     <exception-mapping exception="java.lang.Exception" result="Exception"/> 
     <exception-mapping exception="java.lang.NullPointerException" result="Exception"/> 
    </global-exception-mappings> 

回答

1

該動作是如何定義你的攔截器堆棧我的默認包定義的頂部?應該首先在堆棧中定義ExceptionMappingInterceptor。你能從struts.xml發佈攔截器堆棧配置嗎?你的自定義攔截器不應該干涉(它什麼都不做)。

更新時間:

我能重現此問題,但它有或沒有你的自定義攔截器時我。

原因是您正在尋找異常消息,該消息未被設置爲自動拋出的NullPointerException(如您的情況)。您可以通過替代顯示堆棧跟蹤來確認此問題,例如:%{exceptionStack}

%{exception.message}對於NullPointerException爲空,因此它什麼都不顯示。相反,如果您要拋出一條異常消息(例如,throw new RuntimeException("OMG!");),那麼您將看到該消息。

另外,請注意,您必須在struts.xml中較少具體的映射之前指定更具體的異常映射。因爲NullPointerExceptionException更具體,所以您必須先列出它。請注意,在您的示例中,這並不重要,因爲它們映射到相同的東西。只要知道你的NPE將映射到第一個入口,而不是第二個入口。

+0

看到上面,謝謝。 – WSkinner 2010-11-16 15:41:04

+0

在Tomcat 6.x上使用Struts 2.2.1進行了確認。 – 2010-11-16 23:44:31