如何使用JSF/MyFaces根据用户角色创建条件? [英] How to create conditions based on user role using JSF/MyFaces?

查看:108
本文介绍了如何使用JSF/MyFaces根据用户角色创建条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从JSP页面读取当前用户的角色的哪些选项?我知道Tomahawk组件上的visibleOnUserRole="myRole"属性,但是我需要角色来完成比简单可见性更复杂的事情.

What options do I have to read the roles of the current user from my JSP pages? I'm aware of the visibleOnUserRole="myRole" attribute on Tomahawk components, but I need roles for a bit more complicated things than simple visibility.

推荐答案

ExternalContext 公开用户和角色信息.

The ExternalContext exposes user and role information.

public class RolesAccess implements Serializable {

    public String getUserPrincipalName() {
        FacesContext context = FacesContext.getCurrentInstance();
        Principal principal = context.getExternalContext().getUserPrincipal();
        if(principal == null) {
            return null;
        }
        return principal.getName();
    }

    public String getUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        return context.getExternalContext().getRemoteUser();
    }

    public boolean isManager() {
        FacesContext context = FacesContext.getCurrentInstance();
        return context.getExternalContext().isUserInRole("manager");
    }

}

如果您在servlet中使用JSF,此信息将映射到

If you're using JSF in servlets, this information maps to the values exposed by the HttpServletRequest.

您可以使用托管bean通过向视图公开值.表达语言.

You can use managed beans to expose values to the view via the Expression Language.

<f:view>
    <h:outputLabel value="#{rolesBean.userPrincipalName}" />
    <h:outputLabel value="#{rolesBean.user}" />
    <h:outputLabel value="#{rolesBean.manager}" />
</f:view>

这篇关于如何使用JSF/MyFaces根据用户角色创建条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆