2011-12-27 85 views
4

我正在使用Java 6,jsf 1.2,在tomcat上弹出,如果我在某个页面超时后执行操作,我会在下面得到例外。为什么在屏幕上而不是错误页面显示异常?

我的问题是为什么页面不会重定向到我的错误页面/error/error.jsf?

这是web.xml文件(我没有过滤器):

<error-page> 
    <exception-type>javax.faces.application.ViewExpiredException</exception-type> 
    <location>/error/error.jsf</location> 
</error-page> 
    <error-page> 
    <exception-type>java.lang.IllegalStateException</exception-type> 
    <location>/error/error.jsf</location> 
</error-page> 
<error-page> 
    <exception-type>java.lang.Exception</exception-type> 
    <location>/error/error.jsf</location> 
</error-page> 
<error-page> 
    <exception-type>org.springframework.beans.factory.BeanCreationException</exception-type> 
    <location>/error/error.jsf</location> 
</error-page> 

这是我的网页上的错误信息:

 

    An Error Occurred: 
    Error creating bean with name 'melaketViewHandler' defined in 
ServletContext resource [/WEB-INF/JSFViewHandlersContext.xml]: Instantiation 
of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Could not instantiate bean class [com.ewave.meuhedet.view.melaketViewHandlers.MelaketViewHandler]: Constructor threw 
exception; nested exception is java.lang.NullPointerException 

     - Stack Trace 

     org.springframework.beans.factory.BeanCreationException: Error creating bean 
    with name 'melaketViewHandler' defined in ServletContext resource 
    [/WEB-INF/JSFViewHandlersContext.xml]: Instantiation of bean failed; nested 
    exception is org.springframework.beans.BeanInstantiationException: Could not 
    instantiate bean class [com.ewave.meuhedet.view.melaketViewHandlers.MelaketViewHandler]: 
    Constructor threw exception; nested exception is java.lang.NullPointerException 

     at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:254) 
... 

回答

1

我们使用捕捉异常和重定向到错误页面的自定义视图处理程序:

public class ExceptionHandlingFaceletViewHandler extends FaceletViewHandler { 
    ... 

    protected void handleRenderException(FacesContext context, Exception exception) throws IOException, ELException, FacesException { 
    try { 
     if(context.getViewRoot().getViewId().matches(".*/error.jsf")) { 
     /* 
     * This is to protect from infinite redirects if the error page itself is updated in the 
     * future and has an error 
     */ 
     LOG.fatal("Redirected back to ourselves, there must be a problem with the error.xhtml page", exception); 
     return; 
     } 

     String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath(); 
     getHttpResponseObject().sendRedirect(contextPath + "/error"); 
    } 
    catch(IOException ioe) { 
     LOG.fatal("Could not process redirect to handle application error", ioe); 
    } 
    } 

    private HttpServletResponse getHttpResponseObject() { 
    return (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse(); 
    } 
} 
+0

谢谢,它的工作原理。 Wierd,似乎没有ExceptionHandlingFaceletViewHandler,web.xml 标记被简单地忽略,因为facelets捕获到异常。 – 2011-12-27 13:34:08

1

因为异常从来没有因此导致的servlet容器。堆栈跟踪中的某处,您会找到一个处理它的catch

[编辑]为了说明清楚:在servlet的一些代码(的doGet()内)捕捉异常,然后做的e.printStackTrace(out);相当于 - 从来没有看到异常的容器(即,所谓doGet()代码)所以重定向到错误页面的代码永远不会被调用。

如果您使用的是Eclipse:将堆栈跟踪复制到您的IDE中(请参阅Stacktrace Console)。现在你可以点击每个堆栈帧来查看源代码。寻找能够捕捉异常并将其转换为HTML的任何东西。

+0

如果它处理的,为什么它显示的堆栈跟踪? – 2011-12-27 10:25:09

+1

@DonRoby这就是处理,servlet将显示堆栈跟踪。 :) – Thomas 2011-12-27 10:35:52

相关问题