2013-04-18 51 views
4

我想導航到錯誤頁面,如果發生異常。爲此我定義了:沒有重定向到異常錯誤頁面

<error-page> 
    <error-code>500</error-code> 
    <location>/error.jspx</location> 
</error-page> 

web.xml。我也試圖通過Servlet來做到這一點:

<servlet> 
    <servlet-name>ErrorHandler</servlet-name> 
    <servlet-class>web.servlet.ErrorHandler</servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>ErrorHandler</servlet-name> 
    <url-pattern>/errorhandler</url-pattern> 
</servlet-mapping> 
<error-page> 
    <error-code>500</error-code> 
    <location>/errorhandler</location> 
</error-page> 

但無論是導航到error.jspx也不ErrorHandler的Servlet被調用。

爲了測試錯誤處理,我試圖從託管bean的構造函數和actionListener兩個構造函數中嘗試throw new Exception("Test");。但它在控制檯中打印異常,但重定向沒有發生。

我也試過:<exception-type>java.lang.Exception</exception-type>而不是<error-code>500</error-code>,但沒有運氣。我怎樣才能調用Servlet或導航到任何異常發生在任何地方,如構造函數或一些action/actionListener

回答

0

我不知道這是否會爲您服務。 我有一個如下定義的錯誤處理程序。

在 「faces-config.xml中」

<factory> 
    <exception-handler-factory> 
     com.mypackage.global.DatExceptionHandlerFactory 
    </exception-handler-factory> 
    </factory> 

和兩個班

import javax.faces.context.ExceptionHandler; 
import javax.faces.context.ExceptionHandlerFactory; 

public class DatExceptionHandlerFactory extends ExceptionHandlerFactory { 

     private ExceptionHandlerFactory parent; 

     // this injection handles jsf 
     public DatExceptionHandlerFactory(ExceptionHandlerFactory parent) { 
     this.parent = parent; 
     } 

     //create your own ExceptionHandler 
     @Override 
     public ExceptionHandler getExceptionHandler() { 
     ExceptionHandler result = 
      new DatExceptionHandler(parent.getExceptionHandler()); 
     return result; 
     } 
    } 

二等

import java.util.Iterator; 
import javax.faces.FacesException; 
import javax.faces.application.NavigationHandler; 
import javax.faces.context.ExceptionHandler; 
import javax.faces.context.ExceptionHandlerWrapper; 
import javax.faces.context.FacesContext; 
import javax.faces.context.Flash; 
import javax.faces.event.ExceptionQueuedEvent; 
import javax.faces.event.ExceptionQueuedEventContext; 

import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 


public class DatExceptionHandler extends ExceptionHandlerWrapper { 

     private static Log log = LogFactory.getLog(DatExceptionHandler.class); 
     private ExceptionHandler wrapped; 
     public String error = "n"; 


     public String getError() { 
     return error; 
    } 

    public void setError(String error) { 
     this.error = error; 
    } 

    public DatExceptionHandler(ExceptionHandler wrapped) { 
     this.wrapped = wrapped; 
     } 

     @Override 
     public ExceptionHandler getWrapped() { 
     return wrapped; 
     } 

     @Override 
     public void handle() throws FacesException { 
     Iterator iterator = getUnhandledExceptionQueuedEvents().iterator(); 

     while (iterator.hasNext()) { 
      ExceptionQueuedEvent event = (ExceptionQueuedEvent) iterator.next(); 
      ExceptionQueuedEventContext context = (ExceptionQueuedEventContext)event.getSource(); 

      Throwable throwable = context.getException(); 

      FacesContext fc = FacesContext.getCurrentInstance(); 

      try { 
       Flash flash = fc.getExternalContext().getFlash(); 

       // Put the exception in the flash scope to be displayed in the error 
       // page if necessary ... 
       flash.put("errorDetails", throwable.getMessage()); 

       System.out.println("the error is put in the flash: " + throwable.getMessage()); 

       NavigationHandler navigationHandler = fc.getApplication().getNavigationHandler(); 

       navigationHandler.handleNavigation(fc, null, "components/errorHandler.xhtml?faces-redirect=true"); 

       fc.renderResponse(); 
      } finally { 
       iterator.remove(); 
      } 
     } 

     // Let the parent handle the rest 
     getWrapped().handle(); 
     } 
    } 

而且errorHandler.xhtml顯示錯誤

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html"> 

<h:head> 
    <link type="text/css" rel="stylesheet" href="#request.contextPath}/css/default.css" /> 
    <title>#{bundle['guessNumber.error_page.title']}</title> 
</h:head> 

<h:body> 
    <div class="highlighted errorMessage"> 
     <h:outputText escape="false" 
         value="#{bundle['guessNumber.error_page.content']}"/> 
    </div> 
    <br/><br/> 
    <div class="errorDetails"> 
     Error details: <br/> 
     #{flash.keep.errorDetails} 
    </div> 
</h:body> 
</html> 
相關問題