I want to create a menu with a logout link in it.
I used this code:
<p:menubar>
<p:menuitem value="Logout" oncomplete="confirmation.show()"/>
<f:facet name="options">
<p:inputText style="margin-right:10px" />
<p:commandButton type="button" value="Logout" icon="ui-icon-extlink" />
</f:facet>
</p:menubar>
<p:confirmDialog message="Are you sure you want to logout?"
showEffect="bounce" hideEffect="explode"
header="Logout" severity="alert"
widgetVar="confirmation">
<p:commandButton value="Yes Sure" update="messages"
oncomplete="confirmation.hide()"
actionListener="#{authBean.logout}"
/>
<p:commandButton value="Not Yet" onclick="confirmation.hide()"
type="button" />
</p:confirmDialog>
Here is my login and logout methods from authBean:
public String login() {
try{
UserService userService = new UserService();
User currentUser = userService.findUser(username, password);
System.out.println(currentUser);
if (currentUser != null) {
HttpSession session = (HttpSession) FacesContext
.getCurrentInstance().getExternalContext().getSession(true);
session.setAttribute("currentUser", currentUser);
return "index";
}
else return null;
}
catch (Exception e) {
e.printStackTrace();
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, e.toString(), e.toString());
FacesContext.getCurrentInstance().addMessage(null, message);
return null;
}
}
public String logout() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
.getExternalContext().getSession(false);
session.invalidate();
return "login";
}
Here is my faces-config:
<navigation-rule>
<from-view-id>/*</from-view-id>
<navigation-case>
<from-outcome>logout</from-outcome>
<to-view-id>/welcome.xhtml</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
My problem is that when I press Yes on confirm dialog the page doesn't refresh to welcome.xhtml. I have to press on same menu button or any other button so the page updates. I want that it updates to welcome.xhtml after I pressed Yes on confirm dialog.