How to create table dynamically using count and JSTL ForEach

I want to create a dynamic table taking the attributes of the book when it provided no. books to be entered on the previous page. But I am not getting anything.

This is my code:

<table>
<c:forEach begin="1" end= "${ no }" step="1" varStatus="loopCounter">
<tr>
<td>
<input type='text' name="isbn" placeholder="ISBN">
</td>
<td>
<input type="text" name="Title" placeholder="Title">
</td>
<td>
<input type="text" name="Authors" placeholder="Author">
</td>
<td>
<input type="text" name="Version" placeholder="Version">
</td>
</tr>
</c:forEach>
</table>

      

$ {no} is the number of numbers I want to enter. I'm new here. Sorry if the title is not clear. Please, help.

+3


source to share


1 answer


You get nothing because you are not iterating over your list of books. Also, you only print a lot <input type="text" />

on each iteration. Your code should look like this (assuming your list of books lstBooks

and it's already initialized):

<table>
    <!-- here should go some titles... -->
    <tr>
        <th>ISBN</th>
        <th>Title</th>
        <th>Authors</th>
        <th>Version</th>
    </tr>
    <c:forEach begin="1" end= "${ no }" step="1" varStatus="loopCounter"
        value="${lstBooks}" var="book">
    <tr>
        <td>
            <c:out value="${book.isbn}" />
        </td>
        <td>
            <c:out value="${book.title}" />
        </td>
        <td>
            <c:out value="${book.authors}" />
        </td>
        <td>
            <c:out value="${book.version}" />
        </td>
    </tr>
    </c:forEach>
</table>

      


After understanding your problem based on the comments, make sure the variable ${no}

is available in request.getAttribute("no")

. You can check this using a scriptlet (but this is a bad idea ) or simply using <c:out value="${no}" />

.

Please note that, as I said, the variable must be accessible via request.getAttribute

, do not confuse it with request.getParameter

.



By the way, you can set a variable if you know which one might be like this:

<c:set var="no" value="10" />

      

And then you can access it using ${no}

.

More information: JSTL main tag

+4


source







All Articles