idleMonitor
组件,监视用户的操作并在用户再次变为空闲或活动状态时触发。 默认情况下,空闲时间设置为5分钟(300000毫秒),您可以通过timeout属性自定义持续时间,如下所示:
<!-- fire if user idle for 10 seconds -->
<p:idleMonitor timeout="10000" onidle="idleDialog.show()" />
在本教程中,我们将向您展示一个idleMonitor
示例,如果用户闲置10秒钟,则会显示一个确认对话框。 实际上,此示例是受我的银行网站启发的,它总是提示我并询问我是否闲置5分钟后是否继续。
使用的工具 :
- PrimeFaces 3.3
- JSF 2.2.11
- Eclipse 4.2
- Maven 3
- 雄猫7
注意
您可能还会对这个很棒的PrimeFaces idleMonitor展示感兴趣,它向您展示了使用idleMonitor
一种简单而ajax的方式。
1. idleMonitor组件
如果用户闲置10秒钟,将出现一个确认对话框,询问用户是否要继续或注销?
- 如果是,则关闭确认对话框,并通过
growl
组件显示欢迎回来消息。 - 如果用户选择注销,则也关闭确认对话框,并通过
growl
组件显示注销消息。
index.xhtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h1>PrimeFaces idleMonitor example</h1>
<h:form>
<p:growl id="msg" showDetail="true" sticky="true" />
<!-- If idle 10 seconds, run 'idleDialog' -->
<p:idleMonitor timeout="10000" onidle="idleDialog.show()" />
<p:confirmDialog id="confirmDialog"
message="You have been idle for at least 10 seconds,
Please click ok to continue."
header="Are you there?" severity="alert" widgetVar="idleDialog">
<p:commandButton id="confirm" value="Ok" update="msg"
oncomplete="idleDialog.hide()"
actionListener="#{idleBean.welcomeListener}" />
<p:commandButton id="Logout" value="LogMeOut" update="msg"
oncomplete="idleDialog.hide()"
actionListener="#{idleBean.logoutListener}" />
</p:confirmDialog>
</h:form>
</h:body>
</html>
2. ManageBean
提供growl
组件的面Kong消息。
IdleMonitorBean.java
package com.mkyong;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
@ManagedBean(name = "idleBean")
public class IdleMonitorBean {
public void welcomeListener() {
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_WARN, "Welcome Back",
"Continue your works."));
}
public void logoutListener() {
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_WARN,
"You Have Logged Out!",
"Thank you for using abc Online Financial Services"));
// invalidate session, and redirect to other pages
}
}
3.演示
http:// localhost:8080 / primefaces /
下载源代码
下载它– primefaces-IdleMonitor-example.zip (11 KB)
参考文献
标签: 素面
翻译自: https://mkyong.com/jsf2/primefaces/primefaces-idlemonitor-example/