如何在 JSTL c:forEach 中对具有重复列属性的行进行分组
我正在使用 Java 进行编程,我有一个对象列表,我想在 JSTL 中进行迭代,以检查其他元素是否与当前元素有一些相似之处。我想在我的jsp 中完成它,因为这只是一个显示问题。
让我们举一个虚拟的例子,假设我的对象有三个属性:id、lastname 和firstname。 id 将是一个家庭的标识符,我想在 HTML 表中显示该列表,但在迭代我的列表时,我想检查列表的其余部分以查看是否存在其他家庭成员,以便我可以将它们重新分组到一个 td 标记中。
1 | TOTO | James 2 | FOE | Cameron 2 | FOE | Jessica 1 | TOTO | Pat 3 | SAMPLE | Bob
预期结果:
<table>
<tr><td>1</td><td>TOTO</td><td>James, Pat</td></tr>
<tr><td>2</td><td>FOE</td><td>Cameron, jessica</td></tr>
<tr><td>3</td><td>SAMPLE</td><td>Bob</td></tr>
</table>
同样,这是一个基本示例,您可能会想告诉我将我的家庭重新组合到模型层的其他对象中,但我不想这样做。
编辑:我指的是 while 循环,因为我的列表是按 id 排序的,所以我可以轻松检查下一个元素。但其他解决方案对我来说也很好。
I am programming in Java and I have a list of objects I'd like to iterate in JSTL to check if other elements have some similarity with current one. And I'd like to do it in my jsp because it's just a display matter.
Let's take a dummy example and say my object has three properties : id, lastname and firstname. id would be the identifier of a family and I want to display the list in an HTML table BUT while iterating my list I want to check the rest of the list to see if other family members are present so I could regroup them in one td tag.
1 | TOTO | James 2 | FOE | Cameron 2 | FOE | Jessica 1 | TOTO | Pat 3 | SAMPLE | Bob
Expected result :
<table>
<tr><td>1</td><td>TOTO</td><td>James, Pat</td></tr>
<tr><td>2</td><td>FOE</td><td>Cameron, jessica</td></tr>
<tr><td>3</td><td>SAMPLE</td><td>Bob</td></tr>
</table>
Again, this is a basic example and you might be tempted to tell me to regroup my families in an other object in my Model layer but I'd rather not to do that.
Edit: I am implying a while loop because my list is ordered by id so I could easily check the next elements. But other solutions would be fine for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在
Map
中获取已处理的姓氏并对其进行拦截。然而,我同意这应该在(数据)模型端完成,而不是在视图端完成。
You can get hold of processed lastnames in a
Map
and intercept on that.I however agree that this should rather be done in the (data)model side, not in the view side.
您的 bean 应该进行分组,而不是 JSP。
Your bean should do the grouping, not the JSP.