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

5
我的问题很简单(希望可以轻松解决!):如何在使用Vaadin实现的< strong>webservice中完全禁用浏览器缓存?
由于我尝试进行一些PDF流式传输并在浏览器中显示时遇到了问题,因此我想完全禁用缓存。
例如,在这里,我已经阅读了有关解决方案的文章: 使用标记在所有浏览器中关闭缓存? 他们谈论添加一些头文件到Web应用程序以禁用浏览器缓存。但是,我如何将它们添加到我的< strong>Vaadin应用程序中?
一个简短的代码片段将非常受欢迎(并且有帮助!)
再次感谢您与我分享每个答案和想法。
1个回答

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);

如果您想禁用所有Vaadin请求的缓存,您需要查看AbstractApplicationServlet源代码,并扩展如#serveStaticResourcesInVAADIN等方法,这有点棘手,因为其中许多是私有方法。一个更简单的方法可能是使用Http Servlet Filter向响应添加适当的参数,而无需修改您的应用程序。您可以自己编写此方法,这应该很快容易,尽管快速搜索可以找到Apache2许可的Cache-Filter:http://code.google.com/p/cache-filter/wiki/NoCacheFilter。我没有使用过Cache-Filter,但快速浏览表明它对您来说完全可以胜任。

1
请查看此处:https://github.com/vaadin/framework/issues/10909#issuecomment-389649835 - olivmir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接