Search code examples
javaformsstruts2strutsstruts-tags

Preselect value in Struts <s:select> tag?


I have this Struts tag:

<s:select name="country.id" 
          list="countries"  
          listValue="name" 
          listKey="id"
          headerValue="Select Country"
          headerKey=""
          label="Country" />

which outputs the following HTML code:

<select name="country.id" tabindex="12" id="registration_country">
    <option value="">Select Country</option>
    <option value="1">United States</option>
    <option value="2">Afghanistan</option>
    <option value="3">Albania</option>
    <option value="4">Algeria</option>
    ...
    <option value="192">Zambia</option>
    <option value="193">Zimbabwe</option>
</select>

How do I specify that I want, for example, "Albania" to be preselected in the list?


Solution

  • Use the value attribute in the s:select tag:

    <s:select name="country.id" 
    list="countries"  
    listValue="name" 
    listKey="id"
    headerValue="Select Country"
    headerKey=""
    label="Country"
    value="3" />