有没有一种方法可以以编程方式检测用户何时在浏览器中按F5按钮? [英] Is there a way to programatically detect when the user presses F5 button in browser?
问题描述
我有一个缓存,当用户在浏览器中发出F5请求时,我想使该缓存失效.我正在运行JSF 2.0应用程序.有办法吗?
I have a cache that I would like to invalidate whenever user issues a F5 request in the browser. I am running a JSF 2.0 application. Is there a way to do this?
推荐答案
Use the FacesContext.getCurrentInstance().isPostBack()
to check whether a page request is a reload of the same page. An ideal spot for this would be in a <f:viewAction/>
( new with JSF-2.2) or preRenderView
event.
-
定义backing bean方法
Define the backing bean method
public static boolean isPostback() {
return FacesContext.getCurrentInstance().isPostback();
}
使用其中一个
Use either
<f:metadata>
<f:viewAction action="#{bean.isPostBack}" onPostBack="true"/>
</f:metadata>
f:event
<f:metadata>
<f:event type="preRenderView" listener="#{bean.isPostBack}"/>
</f:metadata>
OR
您可以完全跳过整个后备豆isPostBack
的检查,直接在页面上直接执行缓存清除方法.
You could skip the whole backing bean isPostBack
check altogether and directly execute the cache-clearing method directly from the page.
<f:metadata>
<f:viewAction action="#{bean.clearCache}" rendered="#{facesContext.postBack}" onPostBack="true"/>
</f:metadata>
f:event
<f:metadata>
<f:event type="preRenderView" rendered="#{facesContext.postBack}" listener="#{bean.clearCache}"/>
</f:metadata>
此方法的好处是您编写的代码更少,并且当请求为回发请求时,缓存清除机制仍仅执行
The benefit with this approach is that you write less code, and your cache clearing mechanism still executes only when the request is a post back
这篇关于有没有一种方法可以以编程方式检测用户何时在浏览器中按F5按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!