I have some problems with customs error pages and their exception type. I have in my web.xml
this error page;
<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. My problem is when this error happens, the web page does not redirect to my error.xhtml
page.
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
. 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.
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
. If the resource does not exist, then it would usually return a 404 instead of throwing an exception.
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
. This way you can ultimately end up with only the error page on the error code of 404.
No, as you wrote it the error page is shown when your servlet/JSP/JSF/whatever throws a FileNotFoundException
. In the case of a 404, your servlet is not even called so it does not throw anything.
Use this
<error-page>
<error-code>404</error-code>
<location>/the404_page.html</location>
</error-page>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.