Spring 形式:来自字符串的选项标题
很简单的问题。如果我有一个字符串列表,我通过 Springs form:options 标签在下拉列表中呈现该列表,如何将 title 属性的值设置为字符串值?
<form:options items="${listOfString}" title=" ?? "/>
或者我会做一个 forEach,但是可以用 form:options 标签来完成吗?
谢谢!
Pretty simple question. If I have a list of strings, which I render in a dropdown through Springs form:options tag, how do I set the value of the title property to be the strings value?
<form:options items="${listOfString}" title=" ?? "/>
Alternatively I would do a forEach, but can it be done with a form:options tag?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需省略 'title' 属性即可:
You just omit the 'title' attribute:
我假设您的意思是有
itemLabel
和itemValue
参数,并且您还希望有一个itemTitle
参数,以便您可以指定对象上包含要成为 title="" 属性的字符串的字段名称。因此,与此问题相关: https://jira.springsource.org/browse/SPR-7648
如果是这样的话,我发现我必须推出自己的解决方案。这是我为此编写的
.tag
文件:我还包含了设置所选项目的功能。我省略了 htmlEscape 和 css 相关参数,因为我不需要它们,但如果需要,您可以轻松添加它们。
注意:最酷的部分是 SPeL 允许您使用字符串(很像 Javascript)来寻址字段名称,因此如果我们假设
itemValue = "id"
则entry[itemValue]
计算结果为entry.id
。整齐嘿?您可以在此处找到 form:options 标记背后的代码顺便说一句: https://github.com/spring-projects/spring-framework/blob/master/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/OptionsTag。 java
I'm assuming you mean that there are the
itemLabel
anditemValue
params and that you would also like to have anitemTitle
param so you can specify the field name on the object that contains the string to become a title="" attribute.So relating to this issue: https://jira.springsource.org/browse/SPR-7648
If that's the case, I found that I had to roll my own solution. Here's the
.tag
file that I wrote to do it:I've also included capability to set the selected item. I've left out the htmlEscape and css related params because I didn't require them but you could easily add them if required.
Note: the cool part is that SPeL lets you address a field name using a string (much like Javascript) so if we assume
itemValue = "id"
thenentry[itemValue]
evaluates toentry.id
. Neat hey?You can find the code behind the form:options tag here btw: https://github.com/spring-projects/spring-framework/blob/master/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/OptionsTag.java