【问题标题】:How to access "isUserInRole" in jsp using JSTL [duplicate]如何使用JSTL在jsp中访问“isUserInRole” [重复]
【发布时间】:2016-11-23 23:48:15
【问题描述】:

我正在使用 MySQL 制作 JSP/Servlet CRUD。我目前在我的项目中有两个角色:经理和员工。我想根据这些角色显示不同的数据。如何在不使用 scriptlet 的情况下访问 request.isUserInRole() 方法?我听说使用小脚本很糟糕。

我暂时有如下代码:

<c:if <%request.isUserInRole("manager");%>=true> <!-- Display something --> <c:if <%request.isUserInRole("employee");%>=true> <!-- Display something -->

但我得到一个错误:

HTTP Status 500 - /protected/listUser.jsp (line: 97, column: 9) Unterminated &amp;lt;c:if tag

这可能是 JSTL 与 scriptlet 混合的一些问题。

如何在我的 JSP 页面中仅使用 JSTL 访问 isUserInRole() 方法?

【问题讨论】:

    标签: jsp jstl roles


    【解决方案1】:

    您应该使用测试属性(is required)指定条件。

    可以实现

      <% request.setAttribute("isManager", request.isUserInRole("manager")); %>
      <c:if test="${requestScope.isManager}">
        <!-- Display manager -->
      </c:if>
      <c:if test="${!requestScope.isManager}">
        <!-- Display employee -->
      </c:if>
    

    或与

      <% request.setAttribute("isManager", request.isUserInRole("manager")); %>
      <c:choose>
        <c:when test="${requestScope.isManager}">
          <!-- Display manager -->
        </c:when>
        <c:otherwise>
          <!-- Display employee -->
        </c:otherwise>
      </c:choose>
    

    【讨论】:

    • 这只能工作一次。我在&lt;c:if test="${isManager}"&gt; 下放了一个按钮。当我在 GUI 上对其进行测试时,单击后该按钮消失了。如果我重新加载页面,它会返回,但这对用户来说不太友好。有什么帮助吗?
    • 那是另一个问题,你应该创建一个关于它的新帖子。那个按钮是做什么用的(如果合适的话,在问题中添加代码)?使用requestScope&lt;% request.setAttribute("isManager", request.isUserInRole("manager")); %&gt;
    猜你喜欢
    • 2011-06-24
    • 2011-05-06
    • 2011-04-04
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 2022-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多