Struts2 @s.select 标签的列表属性的 Freemarker 哈希
我使用 Freemarker 作为 Struts 2 应用程序的模板引擎,并且在尝试将 Freemarker 哈希传递到 @s.select 标记的列表值时遇到一些问题。
目前,我正在我的模板中尝试类似的操作:
<@s.select name="myDropdown" list={"1":"Foo", "2":"Bar", 3:"Baz"} />
呈现的结果 HTML 是这样的:
<select name="myDropdown" id="myDropdown">
<option value="freemarker.ext.beans.HashAdapter$1$1$1@2c9bebb">freemarker.ext.beans.HashAdapter$1$1$1@2c9bebb</option>
<option value="freemarker.ext.beans.HashAdapter$1$1$1@16ca4a">freemarker.ext.beans.HashAdapter$1$1$1@16ca4a</option>
<option value="freemarker.ext.beans.HashAdapter$1$1$1@173ee8">freemarker.ext.beans.HashAdapter$1$1$1@173ee8</option>
</select>
根据文档,这似乎应该可行,但实际上唯一的示例是使用 Freemarker 列表。哈希只是作为另一种选项提到的,但我还没有找到任何使用它们的代码示例。
最终我的问题是,我应该将哪种 Freemarker 语法与 Struts 2 select 标记一起使用才能呈现以下 HTML?
<select name="myDropdown" id="myDropdown">
<option value="1">Foo</option>
<option value="2">Bar</option>
<option value="3">Baz</option>
</select>
I'm using Freemarker as the templating engine for a Struts 2 application and having some problems trying to pass a Freemarker hash to the @s.select tag's list value.
Currently I'm trying something like this in my template:
<@s.select name="myDropdown" list={"1":"Foo", "2":"Bar", 3:"Baz"} />
The resulting HTML that's rendered is this:
<select name="myDropdown" id="myDropdown">
<option value="freemarker.ext.beans.HashAdapter$1$1$1@2c9bebb">freemarker.ext.beans.HashAdapter$1$1$1@2c9bebb</option>
<option value="freemarker.ext.beans.HashAdapter$1$1$1@16ca4a">freemarker.ext.beans.HashAdapter$1$1$1@16ca4a</option>
<option value="freemarker.ext.beans.HashAdapter$1$1$1@173ee8">freemarker.ext.beans.HashAdapter$1$1$1@173ee8</option>
</select>
Based on the documentation it seems like this should work, but really the only examples are of using Freemarker lists. Hashes are only mentioned as another option, but I haven't been able to find any code examples that use them.
Ultimately my question is, what Freemarker syntax should I use with the Struts 2 select tag in order to render the following HTML?
<select name="myDropdown" id="myDropdown">
<option value="1">Foo</option>
<option value="2">Bar</option>
<option value="3">Baz</option>
</select>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 select 标签的 listKey 和 listValue 属性似乎可以解决问题。
现在的工作代码是:
似乎应该由标签自动处理,但如果不显式设置这两个附加属性,我就无法让它工作。
Using the listKey and listValue properties of the select tag seems to do the trick.
The working code is now:
Seems like that should be taken care of automatically by the tag, but I was not able to get it to work without explicitly setting those two additional properties.