Spring MVC 中默认自动选择选项标签

发布于 2024-09-08 11:11:10 字数 529 浏览 1 评论 0原文

有人知道如何自动选择选项标签吗?

我检查了 spring 表单标签库,但看不到与呈现 JSP 时默认选择的选项值相关的任何属性。

我基本上有这个:

<p>
    <label for="plantLabel" class="label">Plant:</label> 
    <form:select path="strPlant" >
        <form:option value="-" label="--Select Please--" />
        <form:options items="${plants}" itemLabel="strPlant"
            itemValue="strPlant" />
    </form:select>
</p>

并且,我希望列表中的一个选项(即 items="${plants}" ,说“NeemTree”)在页面加载时显示为已选择。

谢谢

Does anybody have an idea on how to auto select option tag?

I checked at the spring form tag library but cant see any property related to an option value being selected by default when the JSP is rendered.

I basically have this:

<p>
    <label for="plantLabel" class="label">Plant:</label> 
    <form:select path="strPlant" >
        <form:option value="-" label="--Select Please--" />
        <form:options items="${plants}" itemLabel="strPlant"
            itemValue="strPlant" />
    </form:select>
</p>

and, I want an option from the list (ie. items="${plants}" , say 'NeemTree') to be shown as already selected when the page loades.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁的新欢旧爱 2024-09-15 11:11:10

Spring MVC 将根据 path="" 属性计算“选定的选项”。

在上面的情况下,我认为,

要么 path="strPlant" 的值为空

,或者

items="${plants}" 属性表示的数组/集合/映射不包含与 path="strPlant" 值相对应的元素。

例如
如果 path="strPlant" 结果为字符串 NeemTree,则 items="" 必须将 NeemTree 作为元素。

仅当您将 HashMap 实例传递给 items="" 属性时,itemLabel="strPlant"itemValue="strPlant" 才有意义。

Spring 参考文档

The "selected Option" will be calculate by Spring MVC based on the path="" attribute.

In above case I think,

Either the value of path="strPlant" is null

OR

the array/collection/map represented by items="${plants}" attribute does not contain an element corresponding to path="strPlant" value.

e.g.
if path="strPlant" results in String NeemTree, items="" must have NeemTree as element.

itemLabel="strPlant" and itemValue="strPlant" make sense only if you are passing an instance of HashMap to items="" attribute.

Spring Reference Documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文