如何使用count和JSTL ForEach动态地创建表 [英] How to create table dynamically using count and JSTL ForEach

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

问题描述

我想创建一个动态表,接受书属性,当它提供了否。要在前一页输入的书籍。
但我没有收到任何东西。

这是我的代码:

 <表> 
< c:forEach begin =1end =$ {no}step =1varStatus =loopCounter>
< tr>
< td>
< input type ='text'name =isbnplaceholder =ISBN>
< / td>
< td>
< input type =textname =Titleplaceholder =Title>
< / td>
< td>
< input type =textname =Authorsplaceholder =Author>
< / td>
< td>
< input type =textname =Versionplaceholder =Version>
< / td>
< / tr>
< / c:forEach>
< / table>

$ {no}是我想要输入的书数。
我是新来的。对不起,如果标题不清楚。请帮助。

解决方案

你没有得到任何东西,因为你没有迭代你的书籍列表。另外,在每次迭代中,您只打印大量< input type =text/> 。你的代码应该是这样的(假设你的书籍列表是 lstBooks ,它已经被初始化了):

 <表> 
<! - 这里应该有一些标题... - >
< tr>
< th> ISBN< / th>
< th>标题< / th>
< th>作者< / th>
< th>版本< / th>
< / tr>
< c:forEach begin =1end =$ {no}step =1varStatus =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}/>

注意,正如我所说的,变量应该是可通过 request.getAttribute 访问,请勿将其与 request.getParameter 混淆。



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

pre $ < c:set var =novalue =10/>

然后您可以使用 $ {no}



更多信息 JSTL核心标签


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.

解决方案

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆