6

I've come across answers for Struts 2.x but none for 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:

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

Organiccat
  • 5,633
  • 17
  • 57
  • 104

2 Answers2

11

Have you tried to use the value attribute on the <html:select> tag?

<html:select property="status" value="...your status choise here...">
  <html:optionsCollection name="statusList" label="description" value="id" />
</html:select>
  • 1
    I have now. I have one more question, is there a way to get it to work with an object? As it stands I can't use "object.id" I have to use JSP, "<%=object.getId()%>" – Organiccat Aug 08 '11 at 18:31
  • 2
    @Organiccat: If you are using [Struts EL tags](http://struts.apache.org/1.x/struts-el/index.html) then you can use EL expressions, like `${object.id}`. This is also the case for [JSP 2.0 with Struts base](http://www.webkaifa.com/jsp/jakartaStrutsCookbook/059600771x/jakartastrutsckbk-chp-3-sect-2.html) tags. If none of the two above, then I'm afraid `<%=object.getId()%>` is the only way –  Aug 09 '11 at 20:01
  • Thanks, we had to downgrade due to server requirements so I'm stuck with what I've got :) – Organiccat Aug 10 '11 at 19:24
2

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:

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

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