本文介绍了如何根据用户角色有条件地显示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.

推荐答案

您可以只使用以编程方式控制流于JSP的HTML输出。您可以查看当前登录的用户通过角色 的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>

参见:



  • See also:

    • How to disable GET requests to JSP page?
    • Restrict JSP/Servlet access to specific users only
    • 这篇关于如何根据用户角色有条件地显示JSP页面的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 09:14