我使用primefaces dock作为导航栏。我希望它根据用户是否登录来更改其中一个图像。我做了一些事情,但它不起作用,因为我同时看到两个图标,我也无法点击退出按钮。你能给我一些建议吗?
这是导航栏(它在所有页面使用的模板中创建):
<h:body>
<h:form>
<p:dock position="top">
<p:menuitem value="Naslovna" icon="unsecuredimages/naslovna.png"
url="main.xhtml" alt="The image could not be found." />
<p:menuitem value="Register" icon="unsecuredimages/register.png"
url="registration.xhtml" alt="The image could not be found." />
<p:menuitem value="Cesta pitanja" icon="unsecuredimages/faq.png"
url="faq.xhtml" alt="The image could not be found." />
<p:menuitem value="Login" icon="unsecuredimages/login.png" url="login.xhtml" rendered ="securityController.checkLogged() == false"/>
<p:menuitem value="Logout" icon="unsecuredimages/logout.png" action="securityController.logOut()" rendered ="securityController.checkLogged() == true"/>
</p:dock>
</h:form>
这就是securityController Backing bean的样子:
@ManagedBean
@RequestScoped
public class SecurityController {
@EJB
private IAuthentificationEJB authentificationEJB;
...
public boolean checkLogged() {
return authentificationEJB.checkAuthentificationStatus();
}
...
}
在此过程中还涉及到一个EJB:
@Stateful(name = "ejbs/AuthentificationEJB")
public class AuthentificationEJB implements IAuthentificationEJB {
@PersistenceContext
private EntityManager em;
....
// Check if user is logged in
public boolean checkAuthentificationStatus() {
// 1-Check if there is something saved in the session(This means the
// user is logged in)
if ((FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().get("userRole") != null)) {
// 2-If there is not a user already loged, then return false
return true;
}
return false;
}
...
您认为我如何将此功能添加到导航栏?
更新
<p:menuitem value="Login" icon="unsecuredimages/login.png" url="login.xhtml" rendered ="securityController.checkLogged"/>
<p:menuitem value="Logout" icon="unsecuredimages/logout.png" action="securityController.logOut()" rendered ="!securityController.checkLogged"/>
我也改为:
public boolean isCheckLogged() {
return authentificationEJB.checkAuthentificationStatus();
}
这就是导航的样子。如您所见,我没有看到登录或注销图标。
我该如何解决?
答案 0 :(得分:2)
而不是c:if
使用rendered
(或任何其他主要组件)的p:menuitem
属性。
像这样:
<p:menuitem value="Login" icon="unsecuredimages/login.png" url="login.xhtml" rendered="#{securityController.checkLogged}"/>
<p:menuitem value="Logout" icon="unsecuredimages/logout.png" action="securityController.logOut()" rendered="#{!securityController.checkLogged}"/>
您的securityController bean中需要getCheckLogged()
或isCheckLogged()
方法。所以:
public boolean getCheckLogged() {
return authentificationEJB.checkAuthentificationStatus();
}
EL将通过命名约定将securityController.checkLogged
属性引用转换为getter方法调用。