How to apply a JSF2 phaselistener after viewroot gets built?

丶灬走出姿态 提交于 2019-12-05 07:18:15

Register a system event listener for the 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>

Example of a listener:

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;
    }

}

using

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

in a template (facelets) works fine for me. Or are there any problems I'm not aware of..? (I hated this faces-config.xml thingy in jsf and try to avoid it completely in jsf2)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!