SpringLearner
SpringLearner

Reputation: 13854

How to compare 2 values using jstl

I have lists in controller

List l=serviceClientServiceImpl.getSavedParents(clientId); 
uiModel.addAttribute("savedParents",l);
List<Parent> parentList = 
(List<Parent>) serviceClientServiceImpl.getParents("Parent",Long.valueOf(clientId)); 
uiModel.addAttribute("parentList", parentList);

and I am retrieving it in jsp like the follow

<ul style="max-width: 300px;" class="nav nav-pills nav-stacked" id="menuId"> 
<c:forEach items="${parentList}" var="test"> 

<li ><a href="#" value="${test.id}-${test.category}" id="${parent.id}" onclick="return getQuestions(this);" > 
<input type="checkbox" value="${test.id}">${test.name }</a></li> 


</c:forEach> 
</ul> 
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv 

<c:forEach items="${savedParents}" var="test1"> 
<ul style="max-width: 300px;" class="nav nav-pills nav-stacked"> 
<li class="active"><a href="#" value="${test1.id}" onclick="return getQuestions(this);"> 
<input type="checkbox" value="${test1.id}"></a></li> 

</ul> 
</c:forEach>

Now I want to merge the the both and where ever test.id equals to test1.id then I want to add an html class="active"

I tried the following way

<ul style="max-width: 300px;" class="nav nav-pills nav-stacked"  id="menuId">
<c:forEach items="${parentList}" var="test">
<c:forEach items="${savedParents}" var="test1">
<c:when test3="${test.id}==${test1.id }">
  <li ><a href="#" class="active" value="${test.id}-${test.category}" id="${parent.id}" onclick="return getQuestions(this);" >
  <input type="checkbox" value="${test.id}">${test.name }</a></li>

</c:when>
<c:otherwise>
  <li ><a href="#" value="${test.id}-${test.category}" id="${parent.id}" onclick="return getQuestions(this);" >
  <input type="checkbox" value="${test.id}">${test.name }</a></li>
</c:otherwise>

</c:forEach>

  <%-- <li ><a href="#" value="${test.id}-${test.category}" id="${parent.id}" onclick="return getQuestions(this);" >
  <input type="checkbox" value="${test.id}">${test.name }</a></li> --%>


</c:forEach>
 </ul>
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

but I am getting error in eclipse

Multiple annotations found at this line:
    - Missing required attribute 
     "test"
    - Undefined attribute name 
     "test3"
    - Missing required attribute 
     "test"
    - Undefined attribute name 
     "test3"

Can any body help me in this?

Upvotes: 2

Views: 6633

Answers (2)

Braj
Braj

Reputation: 46881

It should be <c:when test= instead of <c:when test3=

try with c:if as well

<c:set var="id1" value="2" />
<c:set var="id" value="2" />

<c:if test="${id == id1 }">
    Equal
</c:if>

OR

<c:set var="id1" value="2" />
<c:set var="id" value="2" />

<c:choose>
    <c:when test="${id == id1 }">
        Equal
    </c:when>
    <c:otherwise>
        Not equal
    </c:otherwise>
</c:choose>

Read more JSTL Core conditional Tag

Note:

  • complete conditional expression must be enclosed inside single ${}
  • c:when tag should be enclosed inside the c:choose tag

The <c:choose> works like a Java switch statement in that it lets you choose between a number of alternatives. Where the switch statement has case statements, the <c:choose> tag has <c:when> tags. A a switch statement has default clause to specify a default action and similar way <c:choose> has <c:otherwise> as default clause.

Upvotes: 2

Adheep
Adheep

Reputation: 1585

Try comparing the strings this way

<c:when test="${test.id == test1.id }">

you could also use the eq instead of ==

<c:when test="${test.id eq test1.id }">

Upvotes: 3

Related Questions