【问题标题】:How to create table dynamically using count and JSTL ForEach如何使用 count 和 JSTL ForEach 动态创建表
【发布时间】:2013-02-09 00:33:54
【问题描述】:

我想创建一个动态表,当它提供了 no 时接受书籍属性。要在前一页输入的书籍。 但我什么也得不到。

这是我的代码:

<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} 是我要输入的图书数量。 我刚来这地方。抱歉,如果标题不清楚。请帮忙。

【问题讨论】:

    标签: jsp foreach jstl


    【解决方案1】:

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

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

    根据 cmets 了解您的问题后,请确保 ${no} 变量在 request.getAttribute("no") 上可用。您可以使用 scriptlet(但这是 bad idea)或仅使用 &lt;c:out value="${no}" /&gt; 进行测试。

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

    顺便说一句,如果你知道它的值可能是这样的,你可以设置一个变量:

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

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

    更多信息:JSTL Core Tag

    【讨论】:

    • 我正在尝试接受输入。这就是我有 的原因。实际上所有的例子都只是基于迭代。是否可以像正常循环一样工作?例如:打印一行 10 次
    • 您想在其中输入您书籍的价值吗?
    • 是的。我想创建这种不接受用户书籍的动态表单。它将输入作为前一页的书籍数量。抱歉信息太少了。
    • 好吧,假设这个 ${no} 变量来自以前的请求作为属性,你的 &lt;c:forEach&gt; 应该作为 for-loop 工作。也许您遇到的问题是 ${no} 变量具有 null0 值。可以在循环前添加&lt;c:out value="${no}" /&gt;做一个简单的测试。
    • 我想我发现了这个问题。 我发现 ${no} 不起作用。我试过 {pageScope.no} 但没有运气。现在“s”变量对我有用。非常感谢您的帮助。只是想用 ${} 作为数字,不用担心。非常感谢您的宝贵时间。
    【解决方案2】:

    假设你已经给出了一个地图列表List&lt;Map&lt;String, Object&gt;&gt;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>
    

    【讨论】:

      【解决方案3】:
      <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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-29
        • 1970-01-01
        • 1970-01-01
        • 2015-06-22
        • 1970-01-01
        相关资源
        最近更新 更多