Question

I've a JSF page that contains the following code:

...
<f:event type="preRenderView" listener="#{page1.PreRenderViewEvent}"/> 
...

Now I need to register the listener (Page1.PreRenderViewEvent method) NOT in the page but within the Page1 class constructor ... Something like:

...
UIViewRoot root = FacesContext.getCurrentInstance().getViewRoot();
root.getListenersForEventClass(javax.faces.event.PreRenderViewEvent.class).add ....
...

How I can complete this code ?
Thanks.

Was it helpful?

Solution

Use UIViewRoot#subscribeToViewEvent().

context.getViewRoot().subscribeToViewEvent(PreRenderViewEvent.class, new MySystemEventListener);

Note that this would be too late if the bean is constructed during render response phase as would happen during GET requests. You'd better just do the very job inside the constructor of the request scoped bean itself (or, cleaner, in a @PostConstruct).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top