JSF and cache-control

阅读更多
进行的项目中遇到这样的问题,我的每一个连接都是连接一台服务器,点击一个连接时候刷新Iframe里页面配置一些参数,切换到另一个服务器连接的时候,Iframe里面列表依然显示着上一台服务器数据列表情况,bean数据没有清空。
在网上查了些资料,方法大概如下:
方案一
在head里添加信息,好像不起作用,可能和bean范围有关,没有细究





方案二
利用filter,很好的思路
One of the most annoying problems with building dynamic websites is how to control page cache.

The most used solution is to use a tag inside the tag. Voilà, problem solved. Wrong! For two reasons:
1. It’s HTML, (most) proxy servers will still cache your pages since they don’t read HTML
2. The HTTP specification does not set any guidelines for Pragma response headers; instead, Pragma request headers are discussed.
Although a few caches may honor this header, the majority won’t, and it won’t have any effect.

However, true HTTP headers are almost always obeyed by any cache. With ADF Faces / JSF it is quite easy to gain control over your caching strategies with a phaselistener:

package nl.iteye.utils;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpServletResponse;

public class NoCachePhaseListener implements PhaseListener {

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

    public void afterPhase(PhaseEvent phaseEvent) {
    }

    public void beforePhase(PhaseEvent phaseEvent) {
        FacesContext facesContext = phaseEvent.getFacesContext();
        HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
        response.addHeader(“Pragma”, “no-cache”);
        response.addHeader(“Cache-Control”, “no-cache”);
        response.addHeader(“Cache-Control”, “no-store”);
        response.addHeader(“Cache-Control”, “must-revalidate”);
        response.addHeader(“Expires”, “Mon, 1 Jan 2006 05:00:00 GMT”);//in the past
    }
}


And don’t forget to add the viewhandler to the faces-config.xml within the tag : nl.iteye.utils.NoCachePhaseListener to make this magic work!

方案三
每次连接之前,代码清除bean
TestBean tBean=((TestBean)FacesUtils.getManagedBean("testBean"));
tBean.tlists = null;


你可能感兴趣的:(Cache,JSF,ITeye,Bean,Servlet)