0

I am having some problems writing a if-then-else condition for my jsp page. I think the issue is with my quotes. Also is it possible to use JSTL in javascript?

Code

 <c:choose>
    <c:when test= "'<%=request.getSession().getAttribute("userName")%>' == Guest">
      <button class="btn" id="getCitizens" onclick="DoGuestAccess()" type="button">Guest Access</button>
    </c:when>
    <c:otherwise>
      <button class="btn" id="getCitizens" onclick="DoUsersAccess()" type="button">User Access</button>
    </c:otherwise>
</c:choose>

Error

org.apache.jasper.JasperException: /WEB-INF/jsp/newpage.jsp (line: 485, column: 71) equal symbol expected
4

1 回答 1

2

这看起来像是测试表达式的问题,试试这个:

<c:choose>
    <c:when test= "${request.getSession().getAttribute("userName").equals("Guest")}">
        <button class="btn" id="getCitizens" onclick="DoGuestAccess()" type="button">Guest Access</button>
    </c:when>
<c:otherwise>
    <button class="btn" id="getCitizens" onclick="DoUsersAccess()" type="button">User Access</button>
</c:otherwise>

此外,Java 使用equalsString 比较的方法。

或更详细:

<% var userName = request.getSession().getAttribute("userName"); %>
<c:choose>
    <c:when test= "${userName.equals("Guest")}">
        <button class="btn" id="getCitizens" onclick="DoGuestAccess()" type="button">Guest Access</button>
    </c:when>
<c:otherwise>
    <button class="btn" id="getCitizens" onclick="DoUsersAccess()" type="button">User Access</button>
</c:otherwise>

于 2013-10-26T16:07:20.680 回答