Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate <form:select> with List<String>?

Tags:

java

spring

jsp

I have a List<String> in the controller which im passing to the view. I need to populate <form:select> with that data.

I tried setting the itemValue attribute to "name" but that did not work.

like image 387
Dragan Avatar asked Mar 21 '12 16:03

Dragan


3 Answers

You can do the following:

<form:select path="selectName">
    <form:option value="0" label="Select an Option" />
    <form:options items="${nameOfList}" />
</form:select>

By providing only the items attribute to the form:options tag, it should make the value and label the value of each String in your list.

like image 116
bsimic Avatar answered Sep 21 '22 12:09

bsimic


you can also try as follows:

<form:select path="country">
<form:option value="NONE" label="--- Select ---" />
<form:options items="${countryList}" itemValue="value" itemLabel="description"/>
</form:select>
like image 6
Rashedul.Rubel Avatar answered Sep 23 '22 12:09

Rashedul.Rubel


protected Map referenceData(HttpServletRequest request) throws Exception {
Map referenceData = new HashMap();
Map<String,String> country = new LinkedHashMap<String,String>();
country.put("US", "United Stated");
country.put("CHINA", "China");
country.put("SG", "Singapore");
country.put("MY", "Malaysia");
referenceData.put("countryList", country);

}

Then

<form:select path="country" items="${countryList}" />
like image 1
José Mendes Avatar answered Sep 20 '22 12:09

José Mendes