0

i want to create a table with dynamic no of rows by this way

<table width="89%" style="margin-left:30px;">                   <%
    for (int arrayCounter = 0; arrayCounter < documentList.size(); arrayCounter++) {

    %>
    <% 
    int test = arrayCounter;
    if((arrayCounter%2)==0)){                       
    %>
    <tr>
    <%
    } %>
     <td style="width:2%">

    </td>
     <td style="width:20%;align:left;">
     </td>

      <td style="width:30%;align:left;">

    </td>
    <% 
     if((arrayCounter%2)==0){
      %>
   </tr>
    <%   } %>

     <%
    }
     %>
     </table>

in my jsp this way it will create 4 rows but according coding function it would create 2 row only if documentlist.size()=4; help me !

Abhishek Nayak
  • 3,732
  • 3
  • 33
  • 64
Prashant Aggarwal
  • 208
  • 1
  • 7
  • 20

3 Answers3

1

Obviously it will create only 2 tow when size is 4, when size is 6 it will create 3 row. remove it statement from loop if you want to create rows equal to number if size

V__
  • 538
  • 10
  • 20
0

remove if statements from loop and create rows normally.

change your loop with this for (int arrayCounter = 0; arrayCounter < (documentList.size()/2); arrayCounter++)

and for last row you can have if statement which will compare if (documentList.size()/2)-1 == arrayCounter).. then you will get what you are looking for

else

for (int arrayCounter = 0; arrayCounter < documentList.size(); arrayCounter++) {

if (documentList.size()/2)-1 == arrayCounter){  create 1 row}else{
create 1st row and then arraycounter ++
create 2nd row and then arraycounter ++

} }

V__
  • 538
  • 10
  • 20
0

Don't use scriptlets in jsp, Jsp is is view layer, use as a view. there is servlet/java-beans to put your all java code.

There is jstl taglib, which has many inbuild functions, use it. you can get it from here

In your case to loop over a list do like this:

  • Add jstl library to your classpath

  • First import jstl taglib in top of your jsp.

  • Then you have jstl tags to use in your jsp.

To import jstl in jsp do like:

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

for looping a List in jstl, there is c:forEach tag, you can use it like:

<c:forEach items="${documentList}" var="doc">
   //here you can access one element by doc like: ${doc}
</c:forEach>

If you want to generate table rows, for each documentList element, then do like:

<table width="89%" style="margin-left:30px;">
<c:forEach items="${documentList}" var="doc" varStatus="loop">
  <tr>
    <td style="width:2%">
      //here if you want loop index you can get like: ${loop.index}
    </td>
    <td style="width:20%;align:left;">
     //if you want to display some property of doc then do like: ${doc.someProperty}, 
      jstl will call getter method of someProperty to get the value.

    </td>
    <td style="width:30%;align:left;">

    </td>
  </tr>
</c:forEach>
</table>

read more here for how to avoid java code in jsp.

Community
  • 1
  • 1
Abhishek Nayak
  • 3,732
  • 3
  • 33
  • 64