如何根据用户角色有条件地显示JSP页面的元素 [英] How to conditionally display elements of JSP page depending on user role

查看:1286
本文介绍了如何根据用户角色有条件地显示JSP页面的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网页上取决于登录用户如何加载菜单?我想使网站的一些菜单将在登录前显示和登录后,会表现出更多的菜单取决于登录用户如果管理员在登录那么如果普通用户在登录那么一些不同的菜单将被添加会出现一些administraive菜单。我想建立使用JSP / Servlet的这个项目。当任何菜单页面总用户点击不会重载只有在显示此菜单的细节描述某些部分将被改变。

How to load menu on webpage depends upon login user? I want to make websites where some menu will show before login and after login it will show more menu depends upon login user if admin is login then some administraive menu will appear if normal user is login then some different menu will be added. I want to build this project using JSP/Servlet. When user click on any menu total page will not be reloaded only some part will be changed where show the details description of this menu.

推荐答案

您可以只使用 JSTL 以编程方式控制流于JSP的HTML输出。您可以查看当前登录的用户通过角色<一个href=\"http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#isUserInRole%28java.lang.String%29\"相对=nofollow> 的HttpServletRequest的isUserInRole#() 它返回一个布尔

You can just use JSTL to programmatically control the flow in the HTML output of the JSP. You can check the role of the currently logged-in user by HttpServletRequest#isUserInRole() which returns a boolean.

当你正在使用的Servlet 3.0,你也可以采取新的EL 2.2的支持与调用参数的方法的好处。所以,这应该做的:

As you're using Servlet 3.0, you'll also be able to take benefit of the new EL 2.2 support of invoking methods with arguments. So, this should do:

<c:if test="${pageContext.request.isUserInRole('admin')}">
    <p>This will be displayed only if the user has the role "admin".</p>
</c:if>
<c:if test="${pageContext.request.isUserInRole('guest')}">
    <p>This will be displayed only if the user has the role "guest".</p>
</c:if>

参见:

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