2011-04-20 22 views
2

我使用primefaces底座作爲導航欄。我希望它根據用戶是否登錄來更改其圖像之一。我做了一些事情,但它不起作用,因爲我同時看到了兩個圖標,而且我也無法點擊註銷按鈕。你能給我一些建議嗎?當用戶登錄時,導航欄圖像應該會改變(JSF 2.0 + 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支持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

我怎樣才能解決這個問題?

回答

2

而不是c:if使用​​(或任何其他的primefaces組件)的rendered屬性。

像這樣:

<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豆一getCheckLogged()isCheckLogged()方法。所以:

public boolean getCheckLogged() { 
    return authentificationEJB.checkAuthentificationStatus(); 
} 

EL將被命名約定翻譯securityController.checkLogged屬性引用一個getter方法調用。

+0

我的確如你所說。看到上面的更新。現在我沒有看到任何圖標。這是爲什麼? – sfrj 2011-04-20 08:57:10

+0

再次檢查。而不是「rendered =」securityController.checkLogged()== false「你必須使用像render =」#{securityController.checkLogged}「。你試過嗎?方法必須以」get「或」is「開頭。 – egbokul 2011-04-20 08:58:30

+0

是的,我確實喜歡看到更新,它說更新不是原來的問題,恐怕錯誤可能出現在EJB中,你認爲如何? – sfrj 2011-04-20 09:00:58