如何在Vaadin中禁用浏览器缓存

Way*_*der 5 pdf caching browser-cache vaadin http-headers

我的问题很简短(希望很容易解决!):如何完全禁用使用vaadin实现的Web服务中的浏览器缓存?

我想完全禁用缓存,因为当我尝试执行一些PDF流并将其显示在我的浏览器中时遇到问题。

例如,我在这里阅读了有关我的问题的解决方案:

使用<meta>标记来关闭所有浏览器中的缓存?

他们谈论将一些标头添加到Web应用程序以禁用浏览器缓存。但是,如何将它们添加到Vaadin应用程序中?

简短的代码片段将非常受欢迎(而且很有帮助!)

再次感谢您的所有回答,并认为您正在与我分享。

Cha*_*ony 5

在我看来,您想在下载PDF文件时禁用缓存。假设您使用DownloadStream来流式传输内容,然后按如下所示设置Content-DispositionCache-Control标头即可。

 DownloadStream stream = new DownloadStream(getStreamSource().getStream(), contentType, filename);
stream.setParameter("Content-Disposition", "attachment;filename=" + filename);
// This magic incantation should prevent anyone from caching the data
stream.setParameter("Cache-Control", "private,no-cache,no-store");    
// In theory <=0 disables caching. In practice Chrome, Safari (and, apparently, IE) all ignore <=0. Set to 1s 
stream.setCacheTime(1000);
Run Code Online (Sandbox Code Playgroud)

如果要为所有 Vaadin请求禁用缓存,则必须查看AbstractApplicationServlet的源,并扩展诸如#serveStaticResourcesInVAADIN和其他方法之类的方法-这很棘手,因为其中很多都是私有方法。

一种更简单的方法可能是使用Http Servlet筛选器将适当的参数添加到响应中,而无需完全修改您的应用程序。您可以自己写这个-应该很容易-尽管快速搜索找到了Apache2许可的Cache-Filter:http : //code.google.com/p/cache-filter/wiki/NoCacheFilter

我没有使用过Cache-Filter,但是快速浏览一下可以看出它对您来说很好用。