仅FileNotFoundException当您实际请求的 URL 与FacesServlet. 所以想象一下FacesServlet被映射到*.jsf,然后打开/somenotexistent.jsf会在 Mojarra 的情况下确实抛出一个子类,FileNotFoundException它确实会匹配你的错误页面。
但是,如果您请求的 URL与 的 URL 模式不匹配FacesServlet,则该请求将由另一个 servlet 处理,通常是容器自己的DefaultServlet. 如果资源不存在,那么它通常会返回 404 而不是抛出异常。
您还想添加另一个错误页面来覆盖它:
<error-page>
<error-code>404</error-code>
<location>/faces/error.xhtml</location>
</error-page>
<error-page>
<exception-type>java.io.FileNotFoundException</exception-type>
<location>/faces/error.xhtml</location>
</error-page>
但是,为了防止这种重复,您还可以考虑使用一个servlet 过滤器,该过滤器捕获FileNotFoundException来自的任何实例,FacesServlet然后正确返回 404。JSF 实用程序库OmniFaces已经有这样一个过滤器,FacesExceptionFilter . 这样,您最终只能得到错误代码 404 上的错误页面。