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

Roger Keays is an artist, an engineer, and a student of life. He has no fixed address and has left footprints on 40-something different countries around the world. Roger is addicted to surfing. His other interests are music, psychology, languages, the proper use of semicolons, and finding good food.

Leave a Comment

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