如何实现在生命周期结束时运行的PhaseListener? [英] How to implement a PhaseListener which runs at end of lifecycle?

查看:117
本文介绍了如何实现在生命周期结束时运行的PhaseListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实现在JSF生命周期结束时运行的PhaseListener?

How can I implement a PhaseListener which runs at end of the JSF lifecycle?

推荐答案

您需要实现 beforePhase() /faces/event/PhaseId.html#RENDER_RESPONSE"rel =" noreferrer> PhaseId_RENDER_RESPONSE .渲染响应是JSF生命周期的最后一个阶段.

You need to implement the PhaseListener interface and hook on beforePhase() of the PhaseId_RENDER_RESPONSE. The render response is the last phase of the JSF lifecycle.

public class MyPhaseListener implements PhaseListener {

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.RENDER_RESPONSE;
    }

    @Override
    public void beforePhase(PhaseEvent event) {
        // Do your job here which should run right before the RENDER_RESPONSE.
    }

    @Override
    public void afterPhase(PhaseEvent event) {
        // Do your job here which should run right after the RENDER_RESPONSE.
    }

}

要使其运行,请在faces-config.xml中进行如下注册:

To get it to run, register it as follows in faces-config.xml:

<lifecycle>
    <phase-listener>com.example.MyPhaseListener</phase-listener>
</lifecycle>


更新上面的阶段侦听器确实适用于整个应用程序.要为特定视图提供阶段侦听器,请使用


Update the above phase listener is indeed applicaiton-wide. To have a phase listener for a specific view, use the beforePhase and/or afterPhase attributes of the <f:view>.

例如

<f:view beforePhase="#{bean.beforePhase}">
    ...
</f:view>

使用

public void beforePhase(PhaseEvent event) {
    if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
        // Do here your job which should run right before the RENDER_RESPONSE.
    }
}

JSF 2.0的另一种方式是使用 <f:event type="preRenderView"> :

A more JSF 2.0 way is by the way using the <f:event type="preRenderView">:

<f:event type="preRenderView" listener="#{bean.preRenderView}" />

使用

public void preRenderView() {
    // Do here your job which should run right before the RENDER_RESPONSE.
}

这篇关于如何实现在生命周期结束时运行的PhaseListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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