JSTL:c:forEach 是否保证 JSP 中的列表排序?
我的 JSP 包含以下代码片段:
<c:forEach items="${rulesForm.rules}" var="rule" varStatus="counter">
<tr id="rules${counter.index}" name="rules[${counter.index}]">
“rules”本身是一个 List
。
当我从 Spring MVC 控制器传回 ModelAndView 对象时,我可以看到列表的顺序是正确的。
然而,当它呈现在屏幕上时,顺序有些随机。我还有 JavaScript 对 DOM 执行一些修改,但我没有看到它执行任何重新排序。 因此,我想知道 c:forEach
是否是罪魁祸首?
My JSP contains the following snippet:
<c:forEach items="${rulesForm.rules}" var="rule" varStatus="counter">
<tr id="rules${counter.index}" name="rules[${counter.index}]">
"rules" itself is a List<Rule>
.
When I pass my ModelAndView
object back from my Spring MVC Controller, I can see that my List is in the correct order.
However, when it is then rendered on screen, the ordering is somewhat random. I also have JavaScript that performs some modifications on the DOM, but I don't see this doing any reordering.
Therefore, I am wondering if c:forEach
is the culprit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我打赌
在大多数实现中使用Iterator
,因此它依赖于底层集合顺序。因此,如果您传递List
,则顺序将被保留,而对于Set
来说则不然。尽管文档没有说明:
考虑一下 - 如果不保留有序集合的顺序,则任何服务器端结果排序都没有意义。
I bet
<c:forEach/>
uses anIterator
in most implementations, therefore it relies on underlying collection order. Thus if you passList
, the order will be preserved, which is not true forSet
s.Although the documentation does not state that:
Think about it - if the order of ordered collection was not preserved, any server-side sorting of results wouldn't have sense.
迭代将取决于传递的
List
类型。因此,您必须检查该类型的List
的文档。Iteration will depend on the type of
List
passed. So you'll have to check the docs of that type ofList
.