Using JSF 2.0
If you want to take some action when starting the application (even if it is not already connected), you can use SystemEventListener and subscribe to PostConstructApplicationEvent.
Listener example:
package listeners; import javax.faces.application.Application; import javax.faces.event.AbortProcessingException; import javax.faces.event.ListenerFor; import javax.faces.event.PostConstructApplicationEvent; import javax.faces.event.SystemEvent; import javax.faces.event.SystemEventListener; public class MySystemListener implements SystemEventListener{ @Override public void processEvent(SystemEvent event) throws AbortProcessingException { System.out.println("started"); } @Override public boolean isListenerForSource(Object source) { return source instanceof Application; } }
To sign, you must include this snippet in faces-config.xml
<application> <system-event-listener> <system-event-listener-class> listeners.MySystemListener </system-event-listener-class> <system-event-class> javax.faces.event.PostConstructApplicationEvent </system-event-class> </system-event-listener> </application>
And if you want to take action when a user logs into a specific page, you can use another system event and the f: event tag to receive a notification before the page displays.
For instance:
... <h:body> <f:event type="preRenderView" listener="#{bean.action}"/> <h:form> </h:form> </h:body> ...
Here is more detailed information on using system events: http://andyschwartz.wordpress.com/2009/07/31/whats-new-in-jsf-2/#system-events .
In JSF 1.2, one of the ways I can get notified is with PhaseListener and check the identifier of the current rendering view.
source share