How to auto select drop down box value in Struts 2
In Struts 2, the HTML drop down box can be rendered via <s:select> tag. To auto select a default value for a drop down box, just declared a “value” attribute in the <s:select> tag, and set the default value accordingly.
1. Java List example
A Java list to generate the select options for the drop down box.
//... public class SelectAction extends ActionSupport{ private List<String> searchEngine; private String yourSearchEngine; //set default value public String getDefaultSearchEngine() { return "yahoo.com"; } public SelectAction(){ searchEngine = new ArrayList<String>(); searchEngine.add("google.com"); searchEngine.add("bing.com"); searchEngine.add("yahoo.com"); searchEngine.add("baidu.com"); } //... }
<s:select> tag to render the HTML drop down box. The value=”defaultSearchEngine” will calle the corresponds Action class getDefaultSearchEngine() method to return a default search engine value.
<s:select label="What's your favor search engine" headerKey="-1" headerValue="Select Search Engines" list="searchEngine" name="yourSearchEngine" value="defaultSearchEngine" />
In this example, the drop down box will auto select the “yahoo.com” as the default option.
2. OGNL List example
Create a drop down box via OGNL expression, and set the default value in the “value” attribute directly.
<s:select label="Select a month" headerKey="-1" headerValue="Select Month" list="#{'1':'Jan', '2':'Feb', '3':'Mar', '4':'Apr'}" name="yourMonth" value="2" />
In this example, the drop down box will auto select the “2″ (Feb) as the default option.







[...] Auto select drop down box value Guide to auto select drop down box value. [...]
[...] The syntaxs are self explanatory, but the “headerKey” and “headerValue“. The “headerKey” is a key for the first item in the drop down list, and the “headerValue” is the value expression for the first item in the drop down list. To auto select a default value for drop down box, read this artcle : Auto select drop down box value in Struts 2 [...]