Reputation: 145
i want to use the idle monitor of primefaces for the session timeout. Properly it works. But the redirect to my login-page (login.xhtml it doesn't work.
I use this the idle-monitor in my body of the template of my xhtml pages: Template.xhtml
<p:idleMonitor timeout="1800000" >
<p:ajax event="idle" listener="#{pageServiceBean.timeout()}" oncomplete="alert('Die Session ist abgelaufen.')"/>
</p:idleMonitor>
The code of the pageServiceBean is: PageServiceBean.xhtml
@ManagedBean
@SessionScoped
/**Die Page-Service Bean wird in der Fußzeile der Seite eingesetzt und beinhaltet den Zurück-Button bzw. den Logout Button*/
public class PageServiceBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 3251724422690388588L;
/**Leitet auf zur Auswahlseite zurück*/
public String cancel(){
return"/sampleForDB.xhtml";
}
/**Leitet zur Loginseite zurück*/
public String cancelSampleButton(){
return"/login.xhtml";
}
public void timeout() throws IOException {
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
FacesContext.getCurrentInstance().getExternalContext().redirect("...login.xhtml");
}
}
I have no redirect to the login.xhtml. But the user ist logged out. So the stuff is not valid any more. The to other function (cancel and cancelSampleButton) works. So they navigate to the correct page.
I looked at the answer from Answer in another post But that don't fixed my problem.
In this post the person speaks to set the time in the web.xml. But i don't no which code did i write in the xhtml.
And i don't know what does the three dots in the redirect method do FacesContext.getCurrentInstance().getExternalContext().redirect("...login.xhtml");
Any idea?
Upvotes: 0
Views: 4494
Reputation: 145
I found a solution.
in the xhtml-page PrimefacsIdleMonitor.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<body>
<h:form>
<p:growl id="msg" showDetail="true" sticky="true" />
<!-- nach 10 Sekunden wird der idelDialog gestartet -->
<p:idleMonitor timeout="10000" onidle="idleDialog.show()" />
<p:confirmDialog id="confirmDialog"
message="Die Session ist abgelaufen. Bitte klicke auf Ok um weiter zu arbeiten."
header="Session ist abgelaufen" severity="alert" widgetVar="idleDialog">
<p:commandButton id="confirm" value="Ok" update="msg"
oncomplete="idleDialog.hide()"
actionListener="#{pageServiceBean.welcomeListener}" />
<p:commandButton id="Logout" value="Abmelden" update="msg"
oncomplete="idleDialog.hide()"
actionListener="#{pageServiceBean.logoutListener}" />
</p:confirmDialog>
</h:form>
</body>
</html>
In any xhtml page where the idle-monitor is used:
<div id="idle">
<div id="id">
<ui:insert name="idle">
<ui:include src="/META-INF/templates/PrimefacesIdleMonitor.xhtml"/>
</ui:insert>
</div>
</div>
In the PageServiceBean.java
public void welcomeListener() {
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_WARN, "Hallo",
"Du bist wieder eingeloggt."));
}
public void logoutListener() throws IOException {
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_WARN,
"Du bist ausgeloggt!", "Bis dann"));
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
ExternalContext ec =FacesContext.getCurrentInstance().getExternalContext();
System.out.println(ec.getRequestContextPath()+"/login.xhtml");
ec.redirect(ec.getRequestContextPath()+"/login.xhtml");
FacesMessage msg = new FacesMessage("Information ", " Die Session ist abgelaufen. Bitte melden sie sich erneut an.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
If the user klick "ok" he can work again. If he klick logout he will be redirect to the login-back. No error or something else is throw.
Upvotes: 0