我在为jsp页面编写if-then-else条件时遇到了一些问题。我认为这个问题与我的引言有关。还可以在javascript中使用JSTL吗?
代码
<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>
错误
org.apache.jasper.JasperException: /WEB-INF/jsp/newpage.jsp (line: 485, column: 71) equal symbol expected
答案 0 :(得分: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使用equals
方法进行字符串比较。
或者更详细:
<% 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>