【问题标题】:Custom exception mapping in web xml does not workWeb xml 中的自定义异常映射不起作用
【发布时间】:2015-08-20 08:14:12
【问题描述】:

我的 JSF 2.2 webapp 在 web.xml 中有一个自定义异常映射,如下所示。

    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <display-name>Project</display-name>

    <!-- Welcome page -->
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <session-config>
       <!-- some codes-->
    </session-config>

    <error-page>
        <exception-type>se.telenor.ocfd.service.api.exception.CustomNotFoundException</exception-type>
        <location>/not_found.xhtml</location>
    </error-page>

    <error-page>
        <error-code>404</error-code>
        <location>/404.xhtml</location>
    </error-page>

    <error-page>
        <location>/500.xhtml</location>
    </error-page>

    <error-page>
        <exception-type>javax.faces.application.ViewExpiredException</exception-type>
        <location>/session_timeout.xhtml</location>
    </error-page>

</web-app>

我的例外是

@ApplicationException
public class CustomNotFoundException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public CustomNotFoundException(String message, Throwable cause) {
        super(message, cause);
    }

    public CustomNotFoundException(String message) {
        super(message);
    }
}

但是当异常发生时它确实是not redirect me to not_found.xhtml页面而不是always redirect to 500.xhtml页面。

如果缺少任何东西,有人可以帮助我吗?

【问题讨论】:

    标签: jsf exception-handling custom-error-pages


    【解决方案1】:

    如果异常包含在另一个异常中,则可能会发生这种情况,例如FacesExceptionELException,取决于谁是第一个捕获实际异常并以包装形式将其进一步委托给调用者的人。然后,这只会将&lt;error-page&gt;FacesExceptionELException 中给定的&lt;exception-type&gt; 完全匹配。

    解决它的一种方法是创建一个servlet filter,它在doFilter() 方法中执行如下操作:

    try {
        chain.doFilter(request, response);
    }
    catch (ServletException e) {
        Throwable cause = e.getRootCause();
    
        if (cause instanceof FacesException || cause instanceof ELException) {
            throw new ServletException(cause.getCause()); // Unwrap and rethrow it.
        }
        else {
            throw e;
        }
    }
    

    通过/* 的 URL 模式将其映射到 FacesServlet 或应用程序范围内。

    如果您碰巧使用 JSF 实用程序库 OmniFaces,可以使用 FacesExceptionFilter 的现成解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 2013-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      相关资源
      最近更新 更多