0

I have simple java code for getting number of all persons added/saved in DB and +1 for adding new person like :

for(int i=1, i<= collection.size() + 1, i++){
      <option value="<%=i>"/>"><%=i></option>
}

but Using JSTL I wanted to come up with :

<c:forEach items="${persons + 1}" var="person">
<option value="<c:out value="${person.personId}"/>">
    <c:out value="${person.personId}"/>
</option>
</c:forEach>

but this ${persons + 1} gives me error. please help me in this case.

1 Answers1

0

ForEach in jstl is the same as foreach in java but just other Syntax:

for(Type : Collection ){
   //functionality
}

in which all your Collection value's will be stored, one by one in a variable which consists of the predefined type Type.

So when you are trying to do persons+1 it is only normal that you get an error because you cannot apply any math functions (+,-,*,/,...) on a Collection.

Think of it as this way:

  • you can easly find the result of 1+1

  • You can't find the result of 1+orange

BenMorel
  • 34,448
  • 50
  • 182
  • 322
MrMe TumbsUp
  • 416
  • 1
  • 4
  • 17