繁体   English   中英

在构建viewroot之后如何应用JSF2 phaselistener?

[英]How to apply a JSF2 phaselistener after viewroot gets built?

在我的JSF2应用程序中,我有一个phaselistener需要在RENDER_RESPONSE之前执行,但是在JSF构建了viewroot之后。

首先,我所做的是在faces-config中注册我的PhaseListener。 然后调用侦听器,但是当我执行beforePhase时, getViewRoot().getChildren()仍为空。

其次,我通过在我的xhtml页面上添加以下内容找到了如何做到这一点:

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

这很好用,但是将它添加到我的每个页面都会非常繁琐。 因此,我正在寻找为我的申请做一次的可能性。

有什么想法可以做到这一点?

在faces-config中为PreRenderViewEvent注册一个系统事件监听器:

<?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>

监听器的示例:

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

}

运用

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

在模板(facelets)中工作正常。 或者有什么问题我不知道..? (我讨厌jsf中的faces-config.xml,并试着在jsf2中完全避免它)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM