4

I want to create a dynamic table accepting book attributes when it has provided the no. of 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 count of number of books I want to enter. I am new here. Sorry if the title is not clear. Please help.

Aditya Peshave
  • 103
  • 2
  • 2
  • 9

3 Answers3

5

You're not getting anything because you're not iterating your list of books. Also, you're only printing lots of <input type="text" /> on each iteration. Your code should look like this (assuming that your list of books is 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 comments, make sure the ${no} variable is available at request.getAttribute("no"). You can test this using a scriptlet (but this is a bad idea) or just using <c:out value="${no}" />.

Note that as I've said, the variable should be accesible through request.getAttribute, do not confuse it with request.getParameter.

By the way, you can set a variable if you know which could be it's value like this:

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

And then you can access to it using ${no}.

More info: JSTL Core Tag

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • i am trying to take input. That's why I have . actually all examples present are only iteration based. is it possible to work as normal loop?? ex: printing one line 10 times – Aditya Peshave Feb 09 '13 at 06:16
  • You want inputs with the values of your books in there? – Luiggi Mendoza Feb 09 '13 at 06:17
  • yes. I want to create this dynamic form which accepts no of books from the user. It takes input as no of books from previous page. Sorry for less info. – Aditya Peshave Feb 09 '13 at 06:20
  • Well, assuming this `${no}` variable comes from previous request as an attribute, your `` should work as a `for-loop`. Maybe the problem you have is that the `${no}` variable has `null` or `0` value. You can make a simple test by adding `` before the loop. – Luiggi Mendoza Feb 09 '13 at 06:23
  • I think I found out the issue. <% int s = Integer.parseInt(request.getParameter("no")); System.out.println(s); %> I found out that the ${no} is not working. I tried {pageScope.no} but no luck. Now the "s" variable is working for me. THank you so much for help. Just wanted to use ${} for number, No worries. Thank you so much for your time. – Aditya Peshave Feb 09 '13 at 06:28
0

Suppose You have given a List of map List<Map<String, Object>> underEmployees

<caption>Emplyess Under You</caption>
<tr>
    <th>Employee Id</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Designation
</tr>
<c:forEach  var="record"  items="${underEmployees}" > 
    <tr>
        <c:forEach var="entry" items= "${record}">
            <th><c:out value="${entry.value}"></c:out></th>
        </c:forEach>

    </tr>
</c:forEach>

0
<table>
  <tr>
    <th>First name</th>
    <th>Last Name</th>
    <th>Age</th>
  </tr>
  <c:forEach items="${students }" var="student">
    <tr>
      <td>${student.firstName}</td>
      <td>${student.lastName }</td>
      <td>${student.age }</td>
    </tr>
  </c:forEach>
</table>
colidyre
  • 4,170
  • 12
  • 37
  • 53