2017-07-25 88 views
-1

我想设置两个变量使用2如果条件,但它不工作。 我下面的代码片段给出:如果条件不工作在JSTL

<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"%> 
+1

你不应该使用'$'内'{}' ..replace'$ {$ role.id ==}'with'$ {role.id ==}' –

回答

1

尝试:

<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> 

边注:比较会在${...}进行评估。你不需要额外$

+0

Thanks @lazyneuron,我已经使用 ' \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t Supporter \t \t \t \t \t Admin' – Amit

+0

很高兴知道! :) – lazyneuron

3

你用错误的语法,如果条件改变这一行

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

我认为它为你工作