如何在 JSP 循环中使用动态集合名称?
嗨,
我需要能够做这样的事情(当然这是行不通的:
<c:forEach var="column" items="${model.${listName}.enabledColumns}">
${column.name}
</c:forEach>
谢谢
HI,
I need to be able to do something like this (of course this doesn't work:
<c:forEach var="column" items="${model.${listName}.enabledColumns}">
${column.name}
</c:forEach>
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用括号:
或者,如果您的
model
实际上是请求中的地图,则:Use brackets:
or, if your
model
is actually a map within the request, then:如果此上下文中的
${listName}
是您的model
的属性(您通常通过model.getListNameX()
访问该属性,则此只能通过反射来实现,而表达式语言对此不支持,但如果您想迭代这些
${listName}
,那么重构您的类似乎会更有帮助。 (假设这些当前是您的model
的属性),为什么不保留一个以枚举常量作为键的映射以及您想要通过${listName}
作为值?(EnumMap
在这里会很有帮助)。然后,您可以将
Enum.values()
设置为请求属性,将它们作为进行迭代。 listName
并具有:If the
${listName}
in this context is a property of yourmodel
(something you would normally access asmodel.getListNameX()
, this would only be possible with reflection, and the Expression Language has no support for that.But it looks like a refactoring of your class would be more helpful. If you want to iterate through those
${listName}
s (assuming these are currently properties of yourmodel
) , why don't you just keep a map with enum constants as keys and those objects you want to reference via${listName}
as values ? (anEnumMap
would be helpful here).Then, you can set the
Enum.values()
as a request attribute, iterate through them aslistName
and have: