如何拦截请求结束?

时间:2009-11-13 16:09:59

标签: jsf

您好,在JSF中我需要在用户的每个请求结束时执行一些操作。我需要某种拦截器,但我不知道该怎么做。我需要帮助。感谢

2 个答案:

答案 0 :(得分:1)

我推荐BalusC的博客:http://balusc.blogspot.com/2006/09/debug-jsf-lifecycle.html

本文介绍如何拦截JSF lifecycle并调试信息。这也可以让您找到您的请求结束的位置。

如果您在此处发布了一些代码,它还可以帮助我们找出真正的问题所在。 以下是调试生命周期所需实现的代码摘录:

package mypackage;

import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

public class LifeCycleListener implements PhaseListener {

    public PhaseId getPhaseId() {
        return PhaseId.ANY_PHASE;
    }

    public void beforePhase(PhaseEvent event) {
        System.out.println("START PHASE " + event.getPhaseId());
    }

    public void afterPhase(PhaseEvent event) {
        System.out.println("END PHASE " + event.getPhaseId());
    }

}

答案 1 :(得分:1)

如果您希望FacesContext可用,则最佳位置是afterPhase内的PhaseID.RENDER_RESPONSE PhaseListener。例如:

public class MyPhaseListener implements PhaseListener {

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

    public void beforePhase(PhaseEvent event) {
        // No operation here.
    }

    public void afterPhase(PhaseEvent event) {
        FacesContext context = event.getFacesContext();
        // Do your thing here with the FacesContext.
    }

}

如果您不需要FacesContext,那么最好的地方就在chain.doFilter(request, response)内的Filter行之后。例如:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
    chain.doFilter(request, response);
    // Do your thing here.
}