Navigation bar image should change when the user logs in(JSF 2.0 + primefaces)
I use a primefaces dock as a nav bar. I want it to change one of its images depending on if the user is logged in or not. I did something but it doesn't work, because i see the two icons at the same time and also i cant click the logout button. 开发者_运维百科Can you give me some advice?
This is the nav bar(It is created in a template all pages use):
<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>
This is how the securityController Backing bean looks like:
@ManagedBean
@RequestScoped
public class SecurityController {
@EJB
private IAuthentificationEJB authentificationEJB;
...
public boolean checkLogged() {
return authentificationEJB.checkAuthentificationStatus();
}
...
}
In the process there is also an EJB involved:
@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;
}
...
What do you think how can i add this feature to my nav bar?
Update
<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"/>
i also changed to:
public boolean isCheckLogged() {
return authentificationEJB.checkAuthentificationStatus();
}
This is how the navigation looks like. As you see i don't see login or logout icons.
How can i fix it?
Instead of c:if
use the rendered
attribute of p:menuitem
(or any other primefaces component).
Like this:
<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}"/>
You will need a getCheckLogged()
or isCheckLogged()
method in your securityController bean. So:
public boolean getCheckLogged() {
return authentificationEJB.checkAuthentificationStatus();
}
EL will translate the securityController.checkLogged
attribute reference to a getter method call by naming convention.
精彩评论