How to apply the JSF2 phaseler after the viewroot is built?

In my JSF2 application, I have a phaseelist that must be executed before RENDER_RESPONSE, but after JSF built the viewroot.

First, I registered my PhaseListener in faces-config. Then the listener is called, but when I execute beforePhase, getViewRoot().getChildren() is still empty.

Secondly, I found how to do this by adding the following to my xhtml pages:

 <f:phaseListener type="be.application.PolicyController" /> 

This works great, but adding this to each of my pages would be very tedious. Therefore, I am looking for the opportunity to do this once for my application.

Any ideas how to do this?

+6
source share
2 answers

Register a system event listener for PreRenderViewEvent in faces-config:

 <?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" version="2.0"> <application> <system-event-listener> <system-event-listener-class>test.PreRenderViewListener</system-event-listener-class> <system-event-class>javax.faces.event.PreRenderViewEvent</system-event-class> </system-event-listener> </application> </faces-config> 

Listener example:

 public class PreRenderViewListener implements SystemEventListener { @Override public void processEvent(SystemEvent event) throws AbortProcessingException { UIViewRoot root = (UIViewRoot) event.getSource(); System.out.println(root.getChildCount()); } @Override public boolean isListenerForSource(Object source) { return true; } } 
+7
source

using

 <f:phaseListener type="de.xxx.listener.HeaderControlPhaseListener" /> 

in the template (facelets) works great for me. Or are there any problems that I don’t know about ...? (I hated this face-config.xml thingy in jsf and tried to completely exclude it from jsf2)

0
source

Source: https://habr.com/ru/post/895325/


All Articles