Differences between action and actionListener

前端 未结 4 1208
走了就别回头了
走了就别回头了 2020-11-21 04:12

What is the difference between action and actionListener, and when should I use action versus actionListener?

4条回答
  •  别那么骄傲
    2020-11-21 04:57

    As BalusC indicated, the actionListener 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 publishes the exception.

    This happens through a call like this:

    context.getApplication().publishEvent(context, ExceptionQueuedEvent.class,                                                          
        new ExceptionQueuedEventContext(context, exception, source, phaseId)
    );
    

    The default listener for this event is the ExceptionHandler which for Mojarra is set to com.sun.faces.context.ExceptionHandlerImpl. 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.

    This ExceptionHandler can be replaced however in faces-config.xml with a custom implementation:

    
       com.foo.myExceptionHandler
    
    

    Instead of listening globally, a single bean can also listen to these events. The following is a proof of concept of this:

    @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");
        }
    
    }
    

    (note, this is not how one should normally code listeners, this is only for demonstration purposes!)

    Calling this from a Facelet like this:

    
        
            
                
            
        
    
    

    Will result in an error page being displayed.

提交回复
热议问题