سؤال

I would like to send user to /login.xhtml if he/she is idle for a given period of time. I have tried PrimeFaces <p:idlemonitor> but can't figure out how to achieve this.

هل كانت مفيدة؟

المحلول

Use the IdleMonitor component <p:idleMonitor>

<p:idleMonitor timeout="3000">
    <p:ajax event="idle" listener="#{idleMonitorBean.processTimeOut()}"/>
</p:idleMonitor>

Note: timeout in millseconds

Then in your listener method just specify the redirect() path.

@Model
public class IdleMonitorBean {

    public void processTimeOut() throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().redirect(
                "/contextroot/index.xhtml");
    }
}

نصائح أخرى

I'd prefer to keep things client side in this situation. PrimeFaces Extensions pe:javascript can help out here. For example:

<p:idleMonitor timeout="...">
  <pe:javascript event="idle"
                 execute="top.location.href='#{request.contextPath}/login.xhtml'"/>
</p:idleMonitor>

You can watch for user activities such as clicks (maybe you consider mouse move or scroll as not idle, totally depends on you).

    var resetActivityTimer = function () {
        if (typeof window.userActivity != 'undefined')
            clearTimeout(window.userActivity);
        window.userActivity = setTimeout(function () {
            window.location.href = 'login.xhtml';
        //number of ms until to be considered idle
        }, 30000);
    };

    $(window).click(function () {
        //if user clicked they are not idle
        resetActivityTimer();
    });

    //initialize timer
    resetActivityTimer();

Note, If you want to consider scroll or mouse moves as activity don't forget to throttle the events.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top