簡體   English   中英

如果條件在JSTL中不起作用

[英]If Condition is Not Working in JSTL

我正在嘗試使用2 if條件設置兩個變量,但是它不起作用。 我的代碼段如下:

<c:forEach items="${user.roles}" var="role">    

        <span>Value : ${role.id }</span> **<!-- Print the value perfectly-->**

        <c:if test="${role.id  == 3}"> 
            <c:set var="admin" value="${role.id}"></c:set>  
            <span>Value : ${val }</span>
        </c:if>             

        <c:if test="${role.id  == 2}">   **<!-- But not work condition here -->**
            <c:set var="supporter" value="${role.id}"></c:set>  
                <span>Value : ${val }</span>                        
        </c:if>
</c:forEach>

<input type="checkbox" name="roles" value="3" ${admin ==3 ? 'checked' : ''}> Supporter
<input type="checkbox" name="roles" value="2" ${supporter ==2 ? 'checked' : ''}> Admin

添加了標簽庫:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

如果條件更改此行,則使用錯誤的語法

<c:if test="${$rold.id == 3}"><c:if test="${role.id == 3}">

我認為它對你有用

嘗試:

<c:forEach items="${user.roles}" var="role">    

        <span>Value : ${role.id }</span> **<!-- Print the value perfectly-->**

        <c:if test="${rold.id  == 3}"> 
            <c:set var="admin" value="${role.id}"></c:set>  
            <span>Value : ${val }</span>
        </c:if>             

        <c:if test="${role.id  == 2}">   **<!-- But not work condition here -->**
            <c:set var="supporter" value="${role.id}"></c:set>  
                <span>Value : ${val }</span>                        
        </c:if>
</c:forEach>

旁注:比較將在${...}之內進行評估。 您不需要額外的$

首先,我想這必須是role.id而不是rold.id:

<c:if test="${$rold.id  == 3}">

此外,您的角色調用必須帶有單個“ $”符號,如下所示:

test="${role.id == 3}"

要么

test="${role.id eq 3}"

在這里,您可以找到有關此的更多信息: http : //www.javatips.net/blog/jstl-check-equals-not-equals

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM