【问题标题】:How to set default value of Struts select tag from the Collection如何从集合中设置 Struts 选择标签的默认值
【发布时间】:2016-07-25 17:48:49
【问题描述】:

我正在使用<html:select> 标签来读取和显示集合中的值。

<tr>
    <td><bean:message key="prompt.my.amount" /></td>
    <td>
        <html:select property="userPref.amount" style="width:170px">
            <html:options collection="myAmts" property="value" labelProperty="label" />
        </html:select>
    </td>
</tr>

Java 文件包含集合值。

public Collection getMyAmts() {
    if (Utils.empty(myAmts)) {
        myAmts = new Vector();
        myAmts.add(new LabelValueBean("ONE", "one"));
        myAmts.add(new LabelValueBean("TWO", "two"));
        myAmts.add(new LabelValueBean("Three", "three"));
        myAmts.add(new LabelValueBean("FOUR", "four"));
    }
    return myAmts;
}

我想在下拉列表中显示值FOUR 作为默认值。

我怎样才能做到这一点?

【问题讨论】:

  • 你为什么不把它放在集合中的第一个呢?
  • 我只是从上面的场景中尝试。
  • 如果您在代码中的某处设置userPref.amount = "FOUR",它应该显示为选定值。

标签: java jsp collections struts struts-tags


【解决方案1】:

默认值在value 属性中设置。

你试过在标签上使用 value 属性吗?

<html:select property="status" value="...your status choise here...">  <html:optionsCollection name="statusList"
label="description" value="id" /> </html:select>

参考资料:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 2016-10-01
    • 2020-03-13
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多