禁用浏览器缓存JSF [英] Disabling browser cache JSF

查看:195
本文介绍了禁用浏览器缓存JSF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在登录页面时出现ViewExpiredException异常,不应该这样.所以我读了一篇关于过滤器的文章(由BalusC推荐).该页面可能是从浏览器的缓存而不是从服务器加载的.所以我已经实现了这段代码

I'm getting a ViewExpiredException when I'm at my login page, it is not supposed to be. So I read a post (recommended by BalusC) on Filters. It might be that the page is loading from browser's cache and not from server. So I've implemented this code

@WebFilter(servletNames={"Faces Servlet"})
public class NoCacheFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpReq = (HttpServletRequest) request;
        HttpServletResponse httpRes = (HttpServletResponse) response;

        if (!httpReq.getRequestURI().startsWith(httpReq.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) { // Skip JSF resources (CSS/JS/Images/etc)
            httpRes.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
            httpRes.setHeader("Pragma", "no-cache"); // HTTP 1.0.
            httpRes.setDateHeader("Expires", 0); // Proxies.
        }

        chain.doFilter(request, response);
    }

    // ...

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void destroy() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

但是当我尝试对其进行测试时,出现以下错误:

but when I try to test it I get the following error:

The module has not been deployed.
    at org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment.deploy(Deployment.java:210)
    at org.netbeans.modules.j2ee.ant.Deploy.execute(Deploy.java:106)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
    at sun.reflect.GeneratedMethodAccessor193.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:390)
    at org.apache.tools.ant.Target.performTasks(Target.java:411)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
    at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:284)
    at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:539)
    at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:153)

推荐答案

在过滤器的初始化(和销毁)过程中,您将引发异常.因此,筛选器的初始化将完全失败,并导致webapp的启动被阻止.删除那些行.您不应故意在其中引发异常.

You're throwing an exception during filter's initialization (and destroy). So filter's initialization will completely fail and cause the webapp's startup to be blocked. Remove those lines. You shouldn't purposefully throw an exception in there.

@Override
public void init(FilterConfig filterConfig) throws ServletException {
    // NOOP.
}

@Override
public void destroy() {
    // NOOP.
}

阅读过滤器的 javadoc 来了解这些方法是干什么的.

Read the filter's javadoc to learn about what those methods are for.

这篇关于禁用浏览器缓存JSF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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