我试图根据User权限加载一个页面列表。我的数据库中有一个存储角色、路径和页面名称的表。
我目前的代码是:
<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/>有什么方法可以让for循环这样做吗?
发布于 2015-11-03 14:03:50
首先,创建一个对象并从bean中返回该对象。假设你的目标是这样的。
public class MenuItem {
private String action;
private String text;
// Getters and setters
}假设您有一个UserBean,它绑定到示例xhtml (user.xhtml)页面。假设您可以从数据库中保存的信息创建这些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"https://stackoverflow.com/questions/33356759
复制相似问题