Search code examples
javajspcollectionsstrutsstruts-tags

How to set default value of Struts select tag from the Collection


I am using <html:select> tag to read and display values from the collection.

<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>

The Java file contains the collection values.

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;
}

I would like to display the value FOUR as a default in the dropdown list.

How can I achieve this?


Solution

  • The default value is set in the value attribute.

    Have you tried to use the value attribute on the tag?

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

    References: