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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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
.
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
回答2:
You could have different menus in different jsps and then include those jsps based upon the logged in user.
For instance...
<%if(userRole.equals("admin")){%>
<jsp:include page="../menu/admin_menu.jsp" />
<%}%>
<%if(userRole.equals("user")){%>
<jsp:include page="../menu/user_menu.jsp" />
<%}%>