在JSF中使用PrimeFaces进行闲置重定向用户

3
如果用户在一定时间内处于空闲状态,我想将他/她发送到/login.xhtml。我尝试了PrimeFaces <p:idlemonitor>,但无法弄清如何实现。

我不知道你是否有特定的方式来实现这个,但我可以告诉你如何使用JavaScript和jQuery来完成它。 - Iman Mohamadi
Iman Mohamadi,你可以开始了。 - user3575427
3个回答

2

Use the IdleMonitor component <p:idleMonitor>

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

注意:超时时间单位为毫秒

在你的监听方法中指定redirect()路径即可。

@Model
public class IdleMonitorBean {

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

0

您可以监视用户活动,例如点击(也许您认为鼠标移动或滚动不是空闲状态,这完全取决于您)。

    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();

请注意,如果您希望将滚动或鼠标移动视为活动,请不要忘记对事件进行节流处理。

0

在这种情况下,我更喜欢保持客户端的操作。PrimeFaces扩展pe:javascript可以在这里提供帮助。例如:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接