Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As BalusC indicated, the <code>actionListener</code> by default swallows exceptions, but in JSF 2.0 there is a little more to this. Namely, it doesn't just swallows and logs, but actually <strong>publishes</strong> the exception.</p> <p>This happens through a call like this:</p> <pre><code>context.getApplication().publishEvent(context, ExceptionQueuedEvent.class, new ExceptionQueuedEventContext(context, exception, source, phaseId) ); </code></pre> <p>The default listener for this event is the <code>ExceptionHandler</code> which for Mojarra is set to <code>com.sun.faces.context.ExceptionHandlerImpl</code>. This implementation will basically rethrow any exception, except when it concerns an AbortProcessingException, which is logged. ActionListeners wrap the exception that is thrown by the client code in such an AbortProcessingException which explains why these are always logged.</p> <p>This <code>ExceptionHandler</code> can be replaced however in faces-config.xml with a custom implementation:</p> <pre><code>&lt;exception-handlerfactory&gt; com.foo.myExceptionHandler &lt;/exception-handlerfactory&gt; </code></pre> <p>Instead of listening globally, a single bean can also listen to these events. The following is a proof of concept of this:</p> <pre><code>@ManagedBean @RequestScoped public class MyBean { public void actionMethod(ActionEvent event) { FacesContext.getCurrentInstance().getApplication().subscribeToEvent(ExceptionQueuedEvent.class, new SystemEventListener() { @Override public void processEvent(SystemEvent event) throws AbortProcessingException { ExceptionQueuedEventContext content = (ExceptionQueuedEventContext)event.getSource(); throw new RuntimeException(content.getException()); } @Override public boolean isListenerForSource(Object source) { return true; } }); throw new RuntimeException("test"); } } </code></pre> <p><em>(note, this is not how one should normally code listeners, this is only for demonstration purposes!)</em></p> <p>Calling this from a Facelet like this:</p> <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"&gt; &lt;h:body&gt; &lt;h:form&gt; &lt;h:commandButton value="test" actionListener="#{myBean.actionMethod}"/&gt; &lt;/h:form&gt; &lt;/h:body&gt; &lt;/html&gt; </code></pre> <p>Will result in an error page being displayed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload