開発者ドキュメント

PrimeFaces idleMonitorの例

`idleMonitor`コンポーネントは、ユーザーがアイドル状態になったときやアクティブになったときにユーザーのアクションと起動を監視します。デフォルトでは、アイドル時間は5分(300000ミリ秒)に設定されており、次のようなタイムアウト属性で期間をカスタマイズできます。

  <!-- fire if user idle for 10 seconds -->
  <p:idleMonitor timeout="10000" onidle="idleDialog.show()"/>

このチュートリアルでは、 `idleMonitor`の例を見て、ユーザが10秒間アイドル状態になっている場合に確認ダイアログを表示します。実際、この例は私の銀行のウェブサイトに触発されています。私はいつも私にプロンプ​​トを出し、5分間アイドル状態のまま続行するかどうか尋ねます。

使用されるツール:

  1. PrimeFaces 3.3

  2. JSF 2.2.11

  3. Eclipse 4.2

  4. Maven 3

  5. Tomcat 7

1. idleMonitorコンポーネント

ユーザーが10秒間アイドル状態になると、確認ダイアログが表示され、ユーザーは続行するかログアウトするかを尋ねるメッセージが表示されます。

  1. はいの場合は、確認ダイアログを閉じ、ウェルカムバックを表示します

メッセージを `growl`コンポーネント経由で送信します。

  1. ユーザーがログアウトを選択した場合は、確認ダイアログを閉じて、

`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`コンポーネントにfacesメッセージを提供します。

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
    }
}

ソースコードをダウンロードする

ダウンロードする – リンク://wp-content/uploads/2012/08/primefaces-IdleMonitor-example.zip[primefaces-IdleMonitor-example.zip](11 KB)

モバイルバージョンを終了