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.

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
8 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
6 Comment authors
SimbuDixit ShubhLilikaLililkaLilika Recent comment authors
newest oldest most voted
trackback
Struts 2 <s:select> drop down box example

[…] 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 […]

Simbu
Guest
Simbu

This example program is not working for me when i try to access directly select.jsp its throwing the following exception. Please help me somebody. I waiting for your reply.

tag ‘select’, field ‘list’, name ‘yourSearchEngine’: The requested list key ‘searchEngine’ could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} – [unknown location]

I am facing this issue more than one week Please help me.

Dixit Shubh
Guest
Dixit Shubh

in case of it is coming from data base then using select tag how can we do that

I want to select default country as India

Lilika
Guest
Lilika

chino cabro!!!!!!! xD

Lililka
Guest
Lililka

El chinito quiele aloz flito, bien flito, de puelto lico >.<

Lilika
Guest
Lilika

Vete a la mela chino malec?n!

Abhed
Guest
Abhed

Hi,

Thanks for your nice tutorials.
How can I set a bean property as a value. For example I have a pojo emplyee with a property department. In my jsp department is a selection-list and I want to open a employee in edit form.
Can I use tag as a value for value attribute, Like:

<s:form action="addEditEmployeeAction" >
	<s:textfield name="name" label="Name"><s:property value="name"/> </s:textfield>
	<s:select name="department"  label="Department" list="departmentList"
	  headerKey="-1" headerValue="Select your department" value="<s:property value="name"/>"
	  />
  	<s:submit />
</s:form>
trackback
Struts 2 Tutorials

[…] Auto select drop down box value Guide to auto select drop down box value. […]