在JSF2.0中有什么方法可以处理页面unLoad事件吗?当用户离开特定页面时,我想执行一些数据重置吗?
发布于 2012-04-23 04:53:39
没有100%可靠的方式来通知服务器端unload事件。根据浏览器的制造商/版本,要么服务器根本不能被ajax (XMLHttpRequest)请求击中,要么如果ajax请求能够成功完成,您将遇到竞争条件(因为ajax请求被突然中止,因为选项卡/窗口被关闭,因此您冒着服务器永远不会检索完整的ajax请求的风险)。
最好的办法是在服务器端挂接销毁事件。例如,对于@ViewScoped bean,您只需创建一个带有@PreDestroy注释的方法
@ManagedBean
@ViewScoped
public class Bean {
@PreDestroy
public void destroy() {
// This method is called whenever the view scope has been destroyed.
// That can happen when the user navigates away by a POST which is
// invoked on this bean, or when the associated session has expired.
}
}或者你根本不需要它。您只需要将数据存储为视图范围内的bean的属性,而不是会话范围内的bean。滥用会话作用域beans的开发人员通常有这种需求;)另请参阅How to choose the right bean scope?
https://stackoverflow.com/questions/10270744
复制相似问题