如何在 Struts2中设置默认选择值标签?
我是 Struts2 的新手。我有用户表单,角色列有一个下拉列表。当用户窗体处于编辑模式时,存储的值被放入相应的控件中。但我无法将下拉列表设置为默认选定值。我该怎么做呢?
I am new in Struts2. I have user form, role column have a drop-down list. When user form is in edit mode, the stored values are placed into corresponding controls. But I can't set drop-down list by default selected value. How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
除了 Nate 的回答之外,我发现如果键类型是字符串,则需要在值属性中的数据周围加上撇号,以便它识别我的输入值是字符串。
In addition to Nate's answer, I've found that I need to put apostrophes around the data in the value attribute if the key type is a String in order for it to recognize that my input value is a String.
如果 select 标记中的值与 select 标记中列表中的键匹配,Struts 将执行正确的操作并将该值设置为默认值。请注意,类型必须匹配。
https://struts.apache.org/tag-developers/select-tag。 html
If the value in your select tag matches a key from the list in the select tag, Struts will do the correct thing and make that value the default. Note that the types must match.
https://struts.apache.org/tag-developers/select-tag.html
用一个例子来说明这一点:
属性“courseId”
courseName
因此输出将如下所示:
值属性设置为“3”,并且它与中的第三个属性匹配
列表,即 Mechanical
因此这将是下拉列表中的默认选择值,
因此输出 html 将类似于:
希望这有帮助。
To illustrate this in an example :
a property "courseId"
courseName
Hence the output will be like :
The value attribute is set to "3" and it matches the 3rd attribute in
the list, which is Mechanical
Therefore this will be the default selected value in the dropdown,
hence the output html will be like :
Hope this helps.
即使您正确遵循了所有内容并且它没有预先选择,那么您需要确保键的返回类型匹配。
例如,以下列表
不起作用,但
有效。
来自 Struts2 文档:
Even though you follow everything correctly and it doesn't preselect then you need to make sure that return type of the key matches.
e.g. for following list
doesn't work, while
works.
From Struts2 documentation:
在下拉列表中显示所选值的正确方法是在操作类中创建 get 方法:
操作类:
JSP:
Right way to do show the selected value in drop down is to create get method in your action class :
Action Class:
JSP: