建立Viewroot后如何应用JSF2相侦听器? [英] How to apply a JSF2 phaselistener after viewroot gets built?

查看:102
本文介绍了建立Viewroot后如何应用JSF2相侦听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的JSF2应用程序中,我有一个相位监听器,需要在RENDER_RESPONSE之前但在JSF构建viewroot之后执行.

In my JSF2 application I have a phaselistener that needs to be executed before RENDER_RESPONSE but after JSF has built the viewroot.

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

First, what I did is register my PhaseListener in faces-config. The listener then gets called, but when I perform the beforePhase, getViewRoot().getChildren() is still empty.

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

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

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

这很好用,但是将其添加到我的每个页面上将非常繁琐. 因此,我正在寻找一种可能对我的应用程序执行一次此操作.

This works fine, but adding this to each of my pages would be extremely tedious. Hence I'm looking for a possibility to do this once for my application.

有什么想法可以做到吗?

Any ideas how this can be done?

推荐答案

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

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>

侦听器的示例:

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

}

这篇关于建立Viewroot后如何应用JSF2相侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆