在 Struts 1.x 中使用 java 设置选择的默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6932659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set default value of select using java in Struts 1.x
提问by Organiccat
I've come across answers for Struts 2.x but none for struts 1.x.
我遇到过 Struts 2.x 的答案,但没有遇到过 struts 1.x 的答案。
All I need to do is select a default value on page load using 1.x of an HTML:SELECT tag that uses an optioncollector:
我需要做的就是使用使用 optioncollector 的 HTML:SELECT 标记的 1.x 在页面加载时选择一个默认值:
<html:select property="status">
<html:optionsCollection name="statusList" label="description" value="id" />
</html:select>
Seems simple, but I'd like to avoid using javascript for this.
看起来很简单,但我想避免为此使用 javascript。
回答by
Have you tried to use the value
attribute on the <html:select>
tag?
您是否尝试过使用标签value
上的属性<html:select>
?
<html:select property="status" value="...your status choise here...">
<html:optionsCollection name="statusList" label="description" value="id" />
</html:select>
回答by Volodymyr Kret
Default select option in struts 1 behaves pretty strange. As user159088 mentioned "value" parameter is responsible for setting default value. But it works only for hardcode:
struts 1 中的默认选择选项表现得很奇怪。正如 user159088 提到的“value”参数负责设置默认值。但它仅适用于硬编码:
<html:select name="myForm" property="formField.enabled" title="Enabled" styleId="enabled" value="false">
<html:option value="true">true</html:option>
<html:option value="false">false</html:option>
</html:select>
Code snippet above works good - false value selected by default. But "formField.enabled"in value parameter doesn't work:
上面的代码片段效果很好 - 默认选择了 false 值。但是值参数中的“formField.enabled”不起作用:
<html:select name="myForm" property="formField.enabled" title="Enabled" styleId="enabled" value="formField.enabled">
<html:option value="true">true</html:option>
<html:option value="false">false</html:option>
</html:select>
Removing value parameter works good in this case - struts check value from property parameter and select this value by default.
在这种情况下,删除 value 参数效果很好 - struts 检查属性参数中的值并默认选择此值。