Java/Wicket - 如何阻止浏览器缓存页面?
我有一个 Java/Wicket 页面,它生成一个 JNLP 文件来启动我公司的软件。该类可以选择接受一些 url 参数并将它们作为参数嵌入到 JNLP 中。当用户启动此 JNLP 文件时,客户端应用程序将根据这些参数执行某些功能。如果客户端软件已在计算机上运行,则点击 JNLP 页面将尝试通过远程调用向正在运行的客户端提供这些参数,而不是启动新页面。
这部分是我遇到问题的地方。在 IE、Firefox 和 Chrome 上,我可以打开一个新客户端,但尝试再次点击相同的 URL 将返回一个 JNLP 文件。我发现清除浏览器缓存可以修复所有浏览器上的此问题。另外,我似乎无法在 JNLP 类中命中断点,这强化了我的预感:这更多是请求的问题,而不是 Wicket 的奇怪问题。
我将以下代码放入我的页面类中,该代码扩展了 org.apache.wicket.markup.html.WebPage:
@Override
protected void setHeaders(WebResponse response) {
getPageMap().remove(this);
HttpServletResponse httpServletResponse = response.getHttpServletResponse();
if (httpServletResponse != null) {
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.addHeader("Cache-Control", "no-cache,no-store,private,must-revalidate,max-stale=0,post-check=0,pre-check=0");
httpServletResponse.addHeader("Keep-Alive", "timeout=3, max=993");
}
}
这似乎不起作用,因为 Firefox 3.6 似乎仍然缓存结果。 IE 7 可以工作,但只有在尝试我创建的链接几次之后。我对 Web 开发和 Wicket 不太了解,这对我来说是新的,所以我可能错过了一些简单的东西。
TL;DR:如何让 Wicket 页面不在客户端浏览器上缓存?
I have a Java/Wicket page that generates a JNLP file that launches my company's software. This class will optionally take some url parameters and embed them as arguments in the JNLP. When the user launches this JNLP file the client application will perform some function based on those parameters. If the client software is already running on the machine, hitting the JNLP page will instead try to feed these parameters via a remote call to the running client instead of launching a new page.
This part is where I'm having issues. On IE, Firefox and Chrome I could open a new client, but trying to hit the same URL again would instead return a JNLP file. I found that clearing the browser cache fixes this issue on all browsers. Also, I cannot seem to hit breakpoints in the JNLP class, which enforces my hunch that this is more of an issue with the request than something strange with Wicket.
I put the following code in my page class, which extends org.apache.wicket.markup.html.WebPage:
@Override
protected void setHeaders(WebResponse response) {
getPageMap().remove(this);
HttpServletResponse httpServletResponse = response.getHttpServletResponse();
if (httpServletResponse != null) {
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.addHeader("Cache-Control", "no-cache,no-store,private,must-revalidate,max-stale=0,post-check=0,pre-check=0");
httpServletResponse.addHeader("Keep-Alive", "timeout=3, max=993");
}
}
This doesn't seem to work, as Firefox 3.6 still seems to cache the result. IE 7 will work but only after trying the link I create a few times. I don't know a lot about web development and Wicket and this is new to me so it's possible I'm missing something simple.
TL;DR: How do I get a Wicket page to not cache on the client browser?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
一些 Wicket 内部使用的 hack(例如,参见 org.apache.wicket.markup.html.image.NonCachingImage 的源代码)是向 URL 添加随机噪声。
基本上,如果您要生成浏览器调用的 url,则可以添加一个被 Web 应用程序忽略的参数,该参数随机变化并欺骗浏览器忽略其缓存。
A hack used in some of the Wicket internals (see for example the source for
org.apache.wicket.markup.html.image.NonCachingImage
) is to add random noise to the url.Basically, if you're generating the urls that the browser calls, you can add a parameter ignored by the web application that varies randomly and fools the browser into ignoring its cache.
请检查以下页面:
https ://web.archive.org/web/20120104201334/http://palisade.plynt.com:80/issues/2008Jul/cache-control-attributes/
Firefox 应遵守“Cache-Control”标头。
Please check the following page:
https://web.archive.org/web/20120104201334/http://palisade.plynt.com:80/issues/2008Jul/cache-control-attributes/
Firefox should honor the "Cache-Control" header.
我不太了解 Wicket,但是您尝试过使用
WebResponse.setLastModifiedTime(Time time)
吗?我知道 FF 发送一个If-Modified-Since
标头,您的服务器将回复304 Not Modified
或正常响应。对我来说,您的服务器会检查
WebResponse
上的lastModifiedTime
来做出决定,这似乎很自然。如果这没有帮助,我建议您获取适用于 Firefox 的 Firebug 并查看请求和响应。
I don't know Wicket very well but have you tried using
WebResponse.setLastModifiedTime(Time time)
? I know FF sends anIf-Modified-Since
header to which your server would reply with304 Not Modified
or the normal response.It would seem natural to me that your server would check the
lastModifiedTime
onWebResponse
to decide.If that doesn't help I would suggest you get Firebug for Firefox and take a look at the requests and responses.
这适用于 IE、Firefox 等,唯一不能使用的浏览器是 konqueror。
This works with IE, Firefox and so on, the only browser with which it certainly does not work is konqueror.
检票口 6.11.0:
Application.get().getResourceSettings().setDefaultCacheDuration(Duration.NONE);
Wicket 6.11.0:
Application.get().getResourceSettings().setDefaultCacheDuration(Duration.NONE);
您是否尝试过使用 window.location.replace 加载页面?
Have you ever tried to load pages using window.location.replace?