我建立了一个客户端,您可以在其中登录,监视某些内容并注销。一切正常。如果您单击注销按钮,则他将直接注销并将我发送回我的登录页面。单击注销按钮后,会直接发生这种情况。所以我想增加大约3秒的等待时间,然后客户端将我发送回我的登录页面。另外,我想添加一条咆哮弹出消息,例如:“您正在注销...”。
问题是,客户端没有等待3秒钟才将我发送回去,也没有显示对话框。他直接把我送回去。如果我只想显示onclick对话框,那么他就很好,如果我只想等待3秒钟才能登出,那么他就很好,他正在等待。但是当我结合这两件事(弹出和等待秒)时,他没有做,直接将我发回。
public void logout() {
try {
Thread thread = new Thread();
try {
thread.sleep(3000);
ExternalContext ec = FacesContext.getCurrentInstance()
.getExternalContext();
ec.invalidateSession();
ec.redirect(ec.getRequestContextPath() + "/home.xhtml");
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void showLogoutMessage() {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Logout",
"Sie werden abgemeldet!"));
logout();
}
<p:growl id="growl" showDetail="true" sticky="false" life="2000" autoUpdate="true" />
<p:commandButton value="#{navigationBean.abmelden}" ajax="false"
action="#{navigationBean.showLogoutMessage()}" update="growl" styleClass="menubutton"
style="position:relative;top:20px">
</p:commandButton>
有人可以帮助我,也可以给我其他解决方案的提示吗?
最佳答案
您可以使用这种方式:
在commandButton中,使用oncomplete调用javascript函数。
javascript函数在3秒后调用p:remoteCommand,然后remoteCommand调用注销。
此处的代码更改:
public void showLogoutMessage(){
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Logout",
"Sie werden abgemeldet!"));}
public void logout() {
try {
Thread thread = new Thread();
try {
thread.sleep(3000);
ExternalContext ec = FacesContext.getCurrentInstance()
.getExternalContext();
ec.invalidateSession();
ec.redirect(ec.getRequestContextPath() + "/home.xhtml");
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();}
}
xhtml:
<p:growl id="growlmm" showDetail="true" sticky="false" life="2000" autoUpdate="true" />
<p:commandButton value="test" ajax="true"
actionListener="#{templateMB.mm()}" update="growlmm" styleClass="menubutton"
style="position:relative;top:20px" oncomplete="myLogout();">
</p:commandButton>
<p:remoteCommand id="rcom3" name="rclogout" process="@this" action="#{templateMB.goOut()}"/>
<script type="text/javascript">
function myLogout() {
setTimeout('rclogout();', 3000);
}
</script>