Q:

How to debug missing resource/reference on xhmtl?

Sometimes I get an exception on my filter, in line:
chain.doFilter(request, response);
And, after a long search, I'll discover that some xhtml file referenced in another xhtml file was missing, or was not declared in faces-config.xml.
And I search the problem by deleting the elements of the xhtml file and see where the exception stops to occur.
Is there a better way to debug this?

A:

Add an exception handler factory that handles the exceptions, and redirect or forward to an error page or login page. 
Good logging in this will help you find the error easily and help you fix the issue sooner. 
web.xml:
<factory>
    <exception-handler-factory>com.framework.exceptionmgmt.CustomExceptionHandlerFactory</exception-handler-factory>
</factory>

factory:
@Override
public ExceptionHandler getExceptionHandler() {
    return new CustomExceptionHandler(wrapped.getExceptionHandler());
}

/**
 * Returns the wrapped factory.
 */
@Override
public ExceptionHandlerFactory getWrapped() {
    return wrapped;
}

handler: 
 @Override
public void handle() throws FacesException {
    for (final Iterator<ExceptionQueuedEvent> it = getUnhandledExceptionQueuedEvents().iterator(); it.hasNext();) {
        Throwable t = it.next().getContext().getException();
        System.out.println("Exception in page: "+t.getMessage());
        while ((t instanceof FacesException || t instanceof EJBException || t instanceof ELException) && t.getCause() != null) {
            t = t.getCause();
        }

        if (t instanceof FileNotFoundException || t instanceof ViewExpiredException) {

            final ExternalContext externalContext = facesContext.getExternalContext();
            final Map<String, Object> requestMap = externalContext.getRequestMap();
            try {
                // Log the information in the logs
                String message;
                if (t instanceof ViewExpiredException) {
                    final String viewId = ((ViewExpiredException) t).getViewId();
                    message = "View is expired. <a href='/ifos" + viewId + "'>Back</a>";
                } else {
                    message = t.getMessage(); // beware, don't leak internal
                                              // info!
                }
                requestMap.put("errorMsg", message);
                try {

                    HttpServletRequest origRequest = (HttpServletRequest) facesContext.getExternalContext()
                            .getRequest();
                    String requestedURL = origRequest.getRequestURL().toString();
                    resetResponse(facesContext);
                    redirectToCorrectPage(origRequest.getContextPath()+"/error.xhtml",facesContext);

                } catch (final IOException e) {
                }
                facesContext.responseComplete();
            } finally {
                it.remove();
            }
        }else{
            try {
            resetResponse(facesContext);
                Map<String, Object> sessnMap = FacesContext
                        .getCurrentInstance().getExternalContext()
                        .getSessionMap();

                // For checking whether user is logged in or not If not then redirect to login page instead of error page
                Boolean isUserLoggedIn = (Boolean) sessnMap
                        .get("isUserLoggedIn");
                if (isUserLoggedIn == null || !isUserLoggedIn) {
                    redirectToCorrectPage(
                            ((HttpServletRequest) facesContext
                                    .getExternalContext().getRequest()).getContextPath()
                                    + "/error.xhtml",
                            facesContext);
                } else {
                    redirectToCorrectPage(
                            ((HttpServletRequest) facesContext
                                    .getExternalContext().getRequest()).getContextPath()
                                    + "/error.xhtml",
                            facesContext);
                }
            } catch (final IOException e) {
            }finally {
                it.remove();
            }
            facesContext.responseComplete();
        }
    }

