基于用户角色渲染 JSF 组件
如何根据登录用户的角色呈现 JSF 组件?我知道外部上下文公开了原理,但是我应该如何在 JSF 中正确进行渲染?在 JSP 中,它会是这样的:
<% isUserInRole(Roles.ADMIN) { %>
<button>Edit!</button>
<% } %>
How do I write this in JSF the best possible way?我最好的猜测是渲染的属性与返回布尔值的支持 bean 的方法相关联,但是如果我必须仅为管理员呈现一些导航项,那么这将引入不相关的支持 bean...
Glassfish V3.1、JSF 2.x
How do I render JSF components based on a logged in user's role? I know the external context exposes the principals, but how should I do the rendering properly in JSF? In JSP it would be something like
<% isUserInRole(Roles.ADMIN) { %>
<button>Edit!</button>
<% } %>
How do I write this in JSF the best possible way? My best guess is the rendered attribute tied to a backing bean's method that returns a boolean, but that would introduce an irrelevant backing bean if I have to render some navigation items only for admins...
Glassfish V3.1, JSF 2.x
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的
web.xml
被声明为 Servlet 3.0(隐含地与 JSP/EL 2.2 相关),那么您可以利用能够在 EL 中调用带有参数的方法,如
ExternalContext#isUserInRole()< /code>
:
请注意,这需要一个支持 Servlet 3.0 的容器,但由于您使用的是 Glassfish 3(支持 Servlet 3.0),因此它应该可以正常工作。
另请注意,如果您使用 Facelets 而不是 JSP,那么您将拥有
HttpServletRequest
在 EL 中可用作#{request}
,允许您使用以下较短的表达式:If your
web.xml
is declared as Servlet 3.0 (which implicitly relates to JSP/EL 2.2)then you can take benefit of being able to invoke methods with arguments in EL like as
ExternalContext#isUserInRole()
:Note that this requires a Servlet 3.0 capable container, but since you're using Glassfish 3 (which supports Servlet 3.0), it should work without any issues.
Also note that if you're using Facelets instead of JSP, then you've the
HttpServletRequest
available as#{request}
in EL, allowing you the following shorter expression:作为对 @wasimbhalli 的回应,我发现该表达式始终返回 false 有两个原因:
角色名称区分大小写。
rendered="#{facesContext.externalContext.isUserInRole('ADMIN')}"
可能会返回false
,但请尝试rendered="#{facesContext.externalContext.isUserInRole('admin')}"
,或rendered="#{facesContext.externalContext.isUserInRole('Admin')}"。您必须在
web.xml
中定义您的角色(或作为注释)并将其映射到glassfish-web.xml
中。以下是如何在
web.ml
中指定角色以下是如何将身份验证组映射到
glassfish-web.xml
中的角色。在我的测试中,即使名称相同,也有必要进行映射,如我在示例代码中所示。另外,在我的测试中,我尝试仅定义映射并仅在
web.xml
中定义角色,但都不起作用。我需要两者,因为以正确的大小写指定角色名称。In response to @wasimbhalli, there are two reasons I have found that the expression would always return false:
The role name is case sensitive.
rendered="#{facesContext.externalContext.isUserInRole('ADMIN')}"
may returnfalse
, but tryrendered="#{facesContext.externalContext.isUserInRole('admin')}"
, or rendered="#{facesContext.externalContext.isUserInRole('Admin')}".You have to define your roles in both
web.xml
(or as annotations) and map it inglassfish-web.xml
.The following is how to specify a role in
web.ml
The following is how to map the authentication group to the role in
glassfish-web.xml
.In my testing it was necessary to do the mapping even when the names were the same, as I show in my example code. Also in my testing, I tried to only define the mapping and only to define the role in
web.xml
, and neither worked. I needed both, as specifying the role name in the correct case.将角色存储在会话属性中,然后使用呈现的属性进行比较。
例如
rendered="#{yoursessionbean.userRole == Roles.ADMIN}"
Store role in session attribute and just compare that using rendered attribute.
e.g.
rendered="#{yoursessionbean.userRole == Roles.ADMIN}"