【问题标题】:How to apply a JSF2 phaselistener after viewroot gets built?构建 viewroot 后如何应用 JSF2 相位监听器?
【发布时间】:2011-08-18 12:19:52
【问题描述】:

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

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

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

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

这很好用,但是将它添加到我的每个页面会非常乏味。 因此,我正在为我的应用程序寻找一次这样做的可能性。

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

【问题讨论】:

    标签: jsf-2 phaselistener


    【解决方案1】:

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

    【讨论】:

    • 我需要在页面中呈现/显示特定组件后(或显示整个页面后)执行一些操作,是否可以自定义上述代码来执行此操作,或者将在不同的方式?
    【解决方案2】:

    使用

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

    在模板(facelets)中对我来说很好。或者有什么我不知道的问题..? (我讨厌 jsf 中的 faces-config.xml 东西,并尝试在 jsf2 中完全避免它)

    【讨论】:

      猜你喜欢
      • 2012-11-01
      • 2012-01-30
      • 2015-06-04
      • 1970-01-01
      • 2019-08-17
      • 1970-01-01
      • 2012-11-23
      • 1970-01-01
      • 2013-06-27
      相关资源
      最近更新 更多