java - 用户登录时导航栏图像应该改变(JSF 2.0 + primefaces)

标签 java jsf jakarta-ee jsf-2 primefaces

我使用 primefaces 停靠栏作为导航栏。我希望它根据用户是否登录来更改其图像之一。我做了一些事情,但它不起作用,因为我同时看到两个图标,而且我无法单击注销按钮。你能给我一些建议吗?

这是导航栏(它是在所有页面使用的模板中创建的):

<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();
    }

这就是导航的样子。如您所见,我没有看到登录或注销图标。

enter image description here

我该如何解决?

最佳答案

使用 p:menuitem(或任何其他 primefaces 组件)的 rendered 属性代替 c:if

像这样:

<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 方法调用。

关于java - 用户登录时导航栏图像应该改变(JSF 2.0 + primefaces),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5727665/

相关文章:

java - GWT:在服务器端调用与客户端相同的 RPC 方法

java - 如何在jsch中获取输出结果

java - 无法再次访问托管 bean...jsf

java - JSF a4j :ajax and f:ajax are failing when rendering

jsf - JSF 中的 Web 过滤器

java - Struts2自定义登录拦截器

java - 如何用三项来描述一个状态

jsf - 如何显示位于玻璃鱼文件夹中的图像?

java - 安排任务

Java 2D回合制游戏编程: handle 2 mouse clicks per player