本文介绍了如何使用计数和 JSTL ForEach 动态创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个动态表,当它提供了编号时接受书籍属性.要在上一页输入的书籍.但我什么也没得到.

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.

这是我的代码:

<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} 是我想输入的书籍数量.我刚来这地方.如果标题不清楚,请见谅.请帮忙.

${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.

推荐答案

你没有得到任何东西,因为你没有迭代你的书单.此外,您只在每次迭代中打印大量 <input type="text"/> .您的代码应如下所示(假设您的图书列表是 lstBooks 并且已初始化):

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>

根据评论了解您的问题后,确保 ${no} 变量在 request.getAttribute("no") 处可用.您可以使用 scriptlet(但这是一个坏主意)或仅使用 <c 进行测试:out value="${no}"/>.


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}" />.

请注意,正如我所说,该变量应该可以通过 request.getAttribute 访问,不要将其与 request.getParameter 混淆.

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" />

然后您可以使用 ${no} 访问它.

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

更多信息:JSTL 核心标签

这篇关于如何使用计数和 JSTL ForEach 动态创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 08:49