JSF: <f: event> with custom events

I can't figure out if custom events can be used using f: event. Ed Burns' book suggests announcing the @NamedEvent annotation to the Event class and using:

<f:event type="com.foo.bar.myEvent" listener="#{listener} /> 

but the event seems never to be created.

From my point of view, this makes sense, since the component does not know anything about the event, for example. when publishing, so this may only be useful to authors of custom components.

On the other hand, standard components should be able to publish an event if it is obtained from, for example, PostAddToViewEvent. In any case, custom events appear to never be published by standard components.

Am I missing something? Is there a convenient way to use custom events with standard components?

Here is what I wanted to do:

 <h:inputText id="input"> <f:event type="foo.bar.MyCustomEvent" /> </h:inputText> 
 public class MyCustomEvent extends javax.faces.event.PostAddToViewEvent { } 
+4
source share
1 answer

yes, for this you can override any method in jsf render or component class

 public class MyComponent extends HtmlInputText or public class MyRenderer extends TextRenderer @Override public void decode(FacesContext context, UIComponent component) { super.decode(context, component); String sourceName = context.getExternalContext().getRequestParameterMap().get("javax.faces.source"); if(sourceName != null && sourceName.equals(component.getClientId())){ component.queueEvent(new MyEvent(component)); } } 

but in the MyEvent class you need to override some methods

 @Override public PhaseId getPhaseId() { return PhaseId.INVOKE_APPLICATION; } 

which will determine in which face this event will be processed (by default, this is ANY_PHASE and an event trigger in the same phase in which it is registered)

 @Override public boolean isAppropriateListener(FacesListener listener) { return false; } 

if you have a suitable listener, it should return true if you have a suitable listener for MyEvent, then the JSF will call this process processAction (ActionEvent event) of the listener when it raises an event, otherwise it will call the broadcast method in the component class, which must be overridden developer

 @Override public void broadcast(FacesEvent event) throws AbortProcessingException { super.broadcast(event); if(event instanceof MyEvent){ try { processMyEvent(event); } catch (Exception e) { // TODO: handle exception } } } 

Even you can register any event yourself using the event queueEvent (FacesEvent event) method, it will register the regiester event and get the phase in which it will be fired by the getPhaseId () method in the MyEvent class, if the getPhaseId () method is not overridden by devloper, then it will work in the same phase in which he registered

0
source

All Articles