开发者

How to intercept request ends?

Hi in JSF i need to perform some action when each request of the user ends. I need some kind of interceptor but i 开发者_如何学编程don't know how to do it. I need help with this please. Thanks


I recommend BalusC's blog: http://balusc.blogspot.com/2006/09/debug-jsf-lifecycle.html

This article shows you how to intercept the JSF lifecycle and debug the information. This will also make it available for you to find out where your request ends.

If you posted some code here it could also help us find out where the true problem lies. Here is an excerpt of the code that you need to implement to debug the 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());
    }

}


If you want to have the FacesContext available, then the best place is the afterPhase of PhaseID.RENDER_RESPONSE inside a PhaseListener. For example:

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

}

If you don't need the FacesContext, then the best place is after the line chain.doFilter(request, response) inside a Filter. For example:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜