简体   繁体   English

如果FileNotFoundException,则web.xml错误页面不起作用

[英]web.xml error page does not work in case of FileNotFoundException

I have some problems with customs error pages and their exception type. 我在海关错误页面及其异常类型方面遇到一些问题。 I have in my web.xml this error page; 我的web.xml这个错误页面;

<error-page>
    <exception-type>java.io.FileNotFoundException</exception-type>
    <location>/faces/error.xhtml</location>
</error-page>

This error ocurrs when I click in a link and the JSF file does not exist. 当我单击链接并且JSF文件不存在时,会发生此错误。 My problem is when this error happens, the web page does not redirect to my error.xhtml page. 我的问题是发生此错误时,网页无法重定向到我的error.xhtml页面。

How is this caused and how can I solve it? 这是怎么引起的,我该如何解决?

This error page on FileNotFoundException works only if you were actually requesting an URL which matches the URL pattern of the FacesServlet . 仅当您实际请求的URL与FacesServlet的URL模式匹配时, FileNotFoundException此错误页面才有效。 So imagine that the FacesServlet is mapped on *.jsf , then opening /somenotexistent.jsf will in case of Mojarra indeed throw a subclass of FileNotFoundException which would indeed match your error page. 因此,假设FacesServlet映射到*.jsf ,然后打开/somenotexistent.jsf ,以防Mojarra确实抛出FileNotFoundException的子类,该子类的确与您的错误页面匹配。

However, if you're requesting an URL which does not match the URL pattern of the FacesServlet , then the request will be handled by another servlet instead, usually the container's own DefaultServlet . 但是,如果您请求的URL与FacesServlet的URL模式匹配,则该请求将由另一个Servlet处理,通常是容器自己的DefaultServlet If the resource does not exist, then it would usually return a 404 instead of throwing an exception. 如果资源不存在,则通常将返回404而不是引发异常。

You'd like to add another error page to cover that as well: 您还想添加另一个错误页面来解决此问题:

<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>

However, to prevent this duplication, you could also consider to use a servlet filter which catches any instance of FileNotFoundException coming from FacesServlet and then properly returns a 404. JSF utility library OmniFaces has already such a filter, the FacesExceptionFilter . 但是,为防止重复,您还可以考虑使用servlet过滤器 ,该过滤器捕获来自FacesServletFileNotFoundException任何实例,然后正确返回404。JSF实用程序库OmniFaces已经有了这样的过滤器FacesExceptionFilter This way you can ultimately end up with only the error page on the error code of 404. 这样,您最终只能得到错误代码404上的错误页面。

No, as you wrote it the error page is shown when your servlet/JSP/JSF/whatever throws a FileNotFoundException . 不,正如您所写,当您的servlet / JSP / JSF /无论抛出什么FileNotFoundException时,都会显示错误页面。 In the case of a 404, your servlet is not even called so it does not throw anything. 在404的情况下,您的servlet甚至没有被调用,因此它不会抛出任何东西。

Use this 用这个

<error-page>
  <error-code>404</error-code>
  <location>/the404_page.html</location>
</error-page>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM