具有多个 ArrayList/linkedHashmap 的 JSP 表
我不确定我的做法是否正确,但这就是我想做的。将 LinkedHashMap 和 ArrayList(最终还有更多)打印到 JSP 页面的表中。到目前为止,它们的长度相同。我能够将它们的值(例如人们的名字/姓氏)从 LinkedHashMap 获取到表行中。我也可以获取 ArrayList 值。现在,我如何获取这两个值?我必须为它们创建一个单独的对象/类吗?或者将 ArrayList 放入 LinkedHashMap 中?当数据最初来自数据库时,这似乎需要更多工作。是否有特定的 JSTL 语法可以使用两者来迭代表的创建?比如:
<c:forEach items="${pNames}" var="current" >
但是有多个项目?有没有更好的设计方法,因为我不想过于复杂。
顺便说一下,这基本上是一个没有标题的表,并且没有额外的 ArrayList
<c:forEach items="${pNames}" var="current" >
<tr><td>${current.value}, ${current.key}</td>
<td> </td><td> </td></tr>
</c:forEach>
I'm not sure if I'm approaching this right, but here's what I want to do. Print a LinkedHashMap and an ArrayList (and eventually more of these) into a table in a JSP page. So far, they both have the same length. I am able to get their values (e.g. people's first/last names) into table rows from a LinkedHashMap. I can get ArrayList values too.. Now, how do I get both? Do I have to make a separate object/class for them? Or put ArrayLists inside a LinkedHashMap? It seems like more work when data will originally come from a database. Is there certain JSTL syntax to iterate a table's creation using both? Something like:
<c:forEach items="${pNames}" var="current" >
But have multiple items? Is there a better design approach to this, because I don't want to over-complicate.
By the way, here's basically a table minus the header and without an additional ArrayList
<c:forEach items="${pNames}" var="current" >
<tr><td>${current.value}, ${current.key}</td>
<td> </td><td> </td></tr>
</c:forEach>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来你可能把事情复杂化了。如果这些数据全部来自数据库,为什么要使用多种数据结构?
我认为这也取决于您的数据的获取方式。如果您使用 JPA 实体,则可以简单地创建实体对象列表并使用它们动态生成表。例如,如果我有一个 Person 表和一个相应的 person 实体,其中包含名字/姓氏、电子邮件、电话等的 getter 和 setter...我将创建一个 people 对象列表,然后在我的 JSP 页面中使用 JSTL 进行迭代他们:
It seems like you might be over complicating the matter. If this data is all coming from a database, why are you using multiple data structures?
I think it depends on how your data is fetched as well. If you are using JPA entities, you can simply create a list of entity objects and generate your tables on the fly using them. For instance if I have a Person table and a corresponding person entity with getters and setters for first/last name, email, phone, etc ... I would create a List of people objects and then in my JSP page use JSTL to iterate through them: