简体   繁体   中英

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

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.

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM