我的问题很短(希望很容易解决!):我怎样才能完全禁用我的浏览器缓存用vaadin实现的webservice?
我想完全禁用缓存,因为当我尝试进行一些 PDF 流式传输并在浏览器中显示它们时遇到问题。
我已经阅读了有关我的问题的解决方案,例如这里:
使用 标签关闭所有浏览器中的缓存? https://stackoverflow.com/questions/1341089/using-meta-tags-to-turn-off-caching-in-all-browsers
他们讨论了向 Web 应用程序添加一些标头以禁用浏览器缓存。但我如何将它们添加到我的Vaadin应用?
一个简短的代码片段将非常受欢迎(并且很有帮助!)
再次感谢您的每一个回答并认为您正在与我分享。
在我看来,您想在下载 PDF 文件时禁用缓存。假设您正在使用DownloadStream
流式传输内容,然后设置Content-Disposition
and Cache-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);
如果您想禁用缓存allVaadin 请求,您必须查看 AbstractApplicationServlet 的源代码,并扩展方法,例如#serveStaticResourcesInVAADIN
和其他 - 这非常棘手,因为其中很多都是私有方法。
一种更简单的方法可能是使用 Http Servlet Filter 将适当的参数添加到响应中,而无需修改您的应用程序。你可以自己写这个 - 应该很快很容易 - 尽管快速搜索找到了 Apache2 许可的 Cache-Filter :http://code.google.com/p/cache-filter/wiki/NoCacheFilter http://code.google.com/p/cache-filter/wiki/NoCacheFilter
我没有使用过缓存过滤器,但是快速浏览一下就会发现它非常适合您。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)