2015-10-26 17 views
1

我試圖根據User權限加載頁面列表。 我的數據庫中有一個表存儲角色,路徑和頁面名稱。如何在hibernate中加載commandLink列表?

我當前的代碼是:

<h:commandLink action="principal.xhtml?faces-redirect=true">Principal</h:commandLink> 
<br/> 
<h:commandLink action="manterusuario.xhtml?faces-redirect=true">Usuários</h:commandLink> 
<br/> 
<h:commandLink action="manterfuncionalidade.xhtml?faces-redirect=true">Funcionalidades</h:commandLink> 
<br/> 
<h:commandLink action="admin.xhtml?faces-redirect=true">Configurações</h:commandLink> 
<hr/> 

有一些方法,使一個循環做到這一點?

回答

1

首先創建一個Object並從bean返回。可以說你的對象是這樣的。

public class MenuItem { 
    private String action; 
    private String text; 

    // Getters and setters 
} 

並讓說你有被綁定到你的榜樣XHTML(user.xhtml)頁面UserBean。並且假設您可以根據您在數據庫中保存的信息創建MenuItem

@ManagedBean 
@RequestScoped 
public class UserBean { 

    List<MenuItem> menuItemsForRole; 

    // This service is responsible for converting database info to menuItem 
    // @ManagedProperty("#{menuItemService}") // you may want to inject it 
    MenuItemService menuItemService = ...; 

    @PostConstruct 
    public void init(){ 
     menuItemsForRole = menuItemService.getMenuItemsForRole("MY_USER_ROLE"); 
     } 

    // Getters & Setters & other properties 
} 

最後,在你的XHTML文件,你可以做這樣的事情

<ui:repeat var="menuItem" value="#{userBean.menuItemsForRole}" varStatus="status"> 
    <h:commandLink action="#{menuItem.action}">#{menuItem.title}</h:commandLink> 
    <br/> 

    </ui:repeat> 

爲了使用UI必須將它的命名空間添加到您的user.xhtml文件。

xmlns:ui="http://java.sun.com/jsf/facelets"