reset password
Author Message
lishenyu
Posts: 103
Posted 18:44 Nov 07, 2014 |

Dr sun ,

could you tell me how to represent  for(int i=0;i<size();i++) this kind of for loop in jsp ?   because I need to use index i in jsp .

ceejay562
Posts: 25
Posted 18:54 Nov 07, 2014 |

  <c:forEach varStatus="index" items="" var="'>

${index.index}

</c:choose>

${index.index } gives you the index that the loop is in. items take in the array attribute and var is variable for each object in that array. 

xytian
Posts: 70
Posted 18:57 Nov 07, 2014 |
lishenyu wrote:

Dr sun ,

could you tell me how to represent  for(int i=0;i<size();i++) this kind of for loop in jsp ?   because I need to use index i in jsp .

<c:forEach begin="0" end="${ fn:length(something)-1 }" var="i">
                          ....
</c:forEach>

lishenyu
Posts: 103
Posted 22:14 Nov 08, 2014 |
xytian wrote:
lishenyu wrote:

Dr sun ,

could you tell me how to represent  for(int i=0;i<size();i++) this kind of for loop in jsp ?   because I need to use index i in jsp .

<c:forEach begin="0" end="${ fn:length(something)-1 }" var="i">
                          ....
</c:forEach>

in this kind of for loop , the [var="i"]   ,can I use the "i" like this:

${renters[i].borrowtime} ?     renters is my arraylist.

xytian
Posts: 70
Posted 22:24 Nov 08, 2014 |
lishenyu wrote:
xytian wrote:
lishenyu wrote:

Dr sun ,

could you tell me how to represent  for(int i=0;i<size();i++) this kind of for loop in jsp ?   because I need to use index i in jsp .

<c:forEach begin="0" end="${ fn:length(something)-1 }" var="i">
                          ....
</c:forEach>

in this kind of for loop , the [var="i"]   ,can I use the "i" like this:

${renters[i].borrowtime} ?     renters is my arraylist.

Give it a try.

In this case it is more convenient to use <c:forEach items="${renters}" var="renter">  $renter.borrowtime </c:forEach>

 

lishenyu
Posts: 103
Posted 23:52 Nov 08, 2014 |
xytian wrote:
lishenyu wrote:
xytian wrote:
lishenyu wrote:

Dr sun ,

could you tell me how to represent  for(int i=0;i<size();i++) this kind of for loop in jsp ?   because I need to use index i in jsp .

<c:forEach begin="0" end="${ fn:length(something)-1 }" var="i">
                          ....
</c:forEach>

in this kind of for loop , the [var="i"]   ,can I use the "i" like this:

${renters[i].borrowtime} ?     renters is my arraylist.

Give it a try.

In this case it is more convenient to use <c:forEach items="${renters}" var="renter">  $renter.borrowtime </c:forEach>

 

I want to print an arraylist in reverse order , so at first I use the following:

<c:forEach begin="${ fn:length(renters)-1 }" end="0" var="i" step="1">

<tr><td>${renters[i].cin}</td><td>${renters[i].renter}</td></tr>

But the fact is that  the above code doesn't print anything (the arraylist is not empty.) .  So could you tell me what should I do to print the array in reverse order using ~<c:forEach items="${renters}" var="renter">  this kind of for loop that has no index I?

Thank you very much !

xytian
Posts: 70
Posted 03:43 Nov 09, 2014 |
 

I want to print an arraylist in reverse order , so at first I use the following:

<c:forEach begin="${ fn:length(renters)-1 }" end="0" var="i" step="1">

<tr><td>${renters[i].cin}</td><td>${renters[i].renter}</td></tr>

But the fact is that  the above code doesn't print anything (the arraylist is not empty.) .  So could you tell me what should I do to print the array in reverse order using ~<c:forEach items="${renters}" var="renter">  this kind of for loop that has no index I?

Thank you very much !

shouldn't step be -1?

Last edited by xytian at 08:29 Nov 09, 2014.
lishenyu
Posts: 103
Posted 20:06 Nov 09, 2014 |

when I did  step='-1'  ,it shows an error of "~javax.servlet.ServletException: javax.servlet.jsp.JspTagException: 'step' <= 0"

  So step cant be -1 .So use the for loop that has index i  can't print the arraylist with reverse order ?

xytian
Posts: 70
Posted 22:17 Nov 09, 2014 |

Then sort renters in the controller.

You can either implement the "Comparable" interface for the class that renter is an instance of, then do Collections.sort(renters) before pass "renters" to jsp,

or you can use a comparator in Collections.sort.

Last edited by xytian at 22:21 Nov 09, 2014.