title: Global exception handling, should not navigate away. Display error on the page
body: <p>I have implemented Global Exception handling in jsf 2.0 following the code example here:
  <a href="http://www.openlogic.com/wazi/bid/259014/How-to-add-exception-handling-to-JSF-applications" rel="nofollow">http://www.openlogic.com/wazi/bid/259014/How-to-add-exception-handling-to-JSF-applications</a>
  I do not want the user to be navigated to the error page. I want the user to be on the same page and display a message.
  So the code now looks like this:</p>
  
  <pre><code>public class CustomExceptionHandler extends ExceptionHandlerWrapper
  {
      private ExceptionHandler wrapped;
  
      public CustomExceptionHandler(ExceptionHandler wrapped){
          this.wrapped = wrapped;
  
      }
  
      @Override
      public ExceptionHandler getWrapped(){
          return wrapped;
      }
  
      @Override
      public void handle() throws FacesException{
          Iterator iterator = getUnhandledExceptionQueuedEvents().iterator();
  
          while (iterator.hasNext()){
              ExceptionQueuedEvent event = (ExceptionQueuedEvent)iterator.next();
              ExceptionQueuedEventContext context = (ExceptionQueuedEventContext)event.getSource();
              Throwable throwable = context.getException();
              FacesContext fc = FacesContext.getCurrentInstance();
  
              final ExternalContext externalContext = fc.getExternalContext();
              final Map&lt;String, Object&gt; requestMap = fc.getExternalContext().getRequestMap();
              final ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc.getApplication().getNavigationHandler();
              //here you do what ever you want with exception
              try {
              fc.getExternalContext().setResponseStatus(500);
              fc.addMessage(null,
                          new FacesMessage(FacesMessage.SEVERITY_ERROR, "There is an error.", ""));
              }
              finally {
                  //remove it from queue
                  iterator.remove();
              }
          }
          getWrapped().handle();
      }
  }
  </code></pre>
  
  <p>But with this code, neither the page is not navigated nor the message is displayed. 
  If I remove this line of code:</p>
  
  <pre><code>fc.getExternalContext().setResponseStatus(500);
  </code></pre>
  
  <p>Then the user is taken to the next page and the message is displayed on that page.</p>
  
  <p>Any idea how I can show the error message on the same page and not navigate the user away ?</p>
  
  <p>Thanks</p>