JSF java.io.FileNotFoundException: Not Found in ExternalContext as a Resource

By , 7 June 2011

JSF java.io.FileNotFoundException: Not Found in ExternalContext as a Resource

Really annoying. JSF 2/Facelets throws an exception for every request to resource which doesn't exist. You would have thought a 404 response would have made more sense. Here is the stack trace:

GRAVE: El Servlet.service() para servlet Faces Servlet lanzó una excepción
java.io.FileNotFoundException: /apps/foo.xhtml Not Found in ExternalContext as a Resource
	at com.sun.faces.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:224)
	at com.sun.faces.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:265)
	at com.sun.faces.facelets.impl.DefaultFaceletFactory.getMetadataFacelet(DefaultFaceletFactory.java:201)
	at com.sun.faces.application.view.ViewMetadataImpl.createMetadataView(ViewMetadataImpl.java:114)
	at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:227)
	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
	at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:111)
	at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
JSF java.io.FileNotFoundException: Not Found in ExternalContext as a Resource

It's good to have the exception for includes and invalid template paths, but this app is deployed publicly and has to handle all sorts of rubbish requests from bots, bad user agents and broken links.

I tried setting up an error page for this exception, but that didn't work possibly because JSF2 has 'fixed' exception handling with their custom ExceptionHandler. The only workaround for me was to hack my filter to check for Faces resources before the servlet can handle them.

// JSF logs annoying exceptions and if it can't find a given view
if (view.endsWith(".xhtml") && 
       !new File(context.getRealPath(view)).exists()) {
    response.sendError(404, "Cannot find " + view);
} else {
    request.getRequestDispatcher(view).forward(request, response);
}

About Roger Keays

JSF java.io.FileNotFoundException: Not Found in ExternalContext as a Resource

I guess I'd call myself a problem solver. Either that, or I'm some sort of organic machine designed to convert oxygen into carbon dioxide. You could go either way on that. I'm into languages and stuff. I wrote a book. It's okay, I guess. What else? I like reading, swimming, eating, and playing music. Satisfied? Sheesh.

Leave a Comment

Please visit https://rogerkeays.com/jsf-java-io-filenotfoundexception-not-found-in-externalcontext-as-a-resource to add your comments.