jsf 2 - JSF 2 Global exception handling, navigation to error page not happening -


i developing jsf 2.0 based web application. trying implement global exception handler redirect user generic error page whenever exception occurs (e.g. nullpointerexception,servletexception,viewexpiredexception etc.)

whenever npe occurs in app, customnavhandler breakpoint hit , navigationhandler code executed, somehow redirection error page not happening, requested page remains partially rendered. idea wrong here ? 1 info throwing npe deliberately on requested page (which partiallyu rendered after npe)

my faces-config.xml entry

<factory>   <exception-handler-factory>     com.common.exceptions.customexceptionhandlerfactory   </exception-handler-factory> </factory> 

my customnavhandler

public class customexceptionhandler extends exceptionhandlerwrapper {  private static final logger logger = logger.getlogger("com.gbdreports.common.exception.customexceptionhandler"); private final exceptionhandler wrapped;  public customexceptionhandler(exceptionhandler wrapped) {     this.wrapped = wrapped; }  @override public exceptionhandler getwrapped() {     return this.wrapped;  } public void handle() throws facesexception {     final iterator<exceptionqueuedevent> = getunhandledexceptionqueuedevents().iterator();               while (i.hasnext()) {                      exceptionqueuedevent event = i.next();                      exceptionqueuedeventcontext context =                                     (exceptionqueuedeventcontext) event.getsource();                        // exception context                      throwable t = context.getexception();                        final facescontext fc = facescontext.getcurrentinstance();            final externalcontext externalcontext = fc.getexternalcontext();         final map<string, object> requestmap = fc.getexternalcontext().getrequestmap();                     final configurablenavigationhandler nav = (configurablenavigationhandler) fc.getapplication().getnavigationhandler();                        //here ever want exception                      try {                                //log error ?                   logger.error("severe exception occured");             //log.log(level.severe, "critical exception!", t);                                //redirect error page                              requestmap.put("exceptionmessage", t.getmessage());                              nav.performnavigation("/testproject/error.xhtml");                              fc.renderresponse();                                // remove comment below if want report error in jsf error message                              //jsfutil.adderrormessage(t.getmessage());                            }          {                              //remove queue                              i.remove();             }                  }              //parent hanle              getwrapped().handle();          }  } 

my customnavhandler factory

public class customexceptionhandlerfactory extends exceptionhandlerfactory {    private exceptionhandlerfactory parent;    public customexceptionhandlerfactory(exceptionhandlerfactory parent) {     this.parent = parent;   }    @override   public exceptionhandler getexceptionhandler() {       return new customexceptionhandler (parent.getexceptionhandler());    }  } 

it's because current request ajax (asynchronous) request. exception handler you've there designed regular (synchronous) requests.

the proper way change view in case of ajax exception follows:

string viewid = "/error.xhtml"; viewhandler viewhandler = context.getapplication().getviewhandler(); context.setviewroot(viewhandler.createview(context, viewid)); context.getpartialviewcontext().setrenderall(true); context.renderresponse(); 

this naive. won't work if ajax exception been thrown in midst of rendering of ajax response.

i'd suggest not reinvent wheel. jsf utility library omnifaces has complete working solution in flavor of fullajaxexceptionhandler. can find full source code here , showcase example here. makes use of standard servlet api <error-page> declarations in web.xml. way error pages reusable synchronous requests, little of facesexceptionfilter, provided omnifaces.

see also:


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -