Set default value of select using java in Struts 1.x
Asked Answered
A

2

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.

Anselme answered 3/8, 2011 at 20:10 Comment(0)
U
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>
Upcountry answered 3/8, 2011 at 20:25 Comment(3)
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()%>"Anselme
@Organiccat: If you are using Struts EL tags then you can use EL expressions, like ${object.id}. This is also the case for JSP 2.0 with Struts base tags. If none of the two above, then I'm afraid <%=object.getId()%> is the only wayUpcountry
Thanks, we had to downgrade due to server requirements so I'm stuck with what I've got :)Anselme
M
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.

Montagna answered 3/11, 2015 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.