我想创建一个带有注销链接的菜单。
我使用了这段代码:
<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>
这是我从 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";
}
这是我的面孔配置:
<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>
我的问题是,当我在确认对话框上按是时,页面不会刷新到welcome.xhtml。我必须按下相同的菜单按钮或任何其他按钮,以便页面更新。我希望它在确认对话框中按“是”后更新为 welcome.xhtml。