I have been trying to find out how to populate a dropdown box in Spring MVC. There are a few threads out there on this subject but none of them that I have found have helped
I have solved this kind of problem today by myself. This is very simple and easy to understand. In Spring MVC 3.0 controller just place this code -
@ModelAttribute("creditCardTypes")
public Map populateCreditCardTypes() {
Map creditCardTypes = new LinkedHashMap();
creditCardTypes.put("VS", "Visa");creditCardTypes.put("MC", "MasterCard");
creditCardTypes.put("AE", "American Express");
creditCardTypes.put("DS", "Discover");creditCardTypes.put("DC", "Diner's Club");
return creditCardTypes;
}
Now "creditCardTypes" attribute will be avaiable in the page loading or page submitting scope , means it will available whatever the requestmapping url would be.
In jsp , place this code inside the - Credit card types:
here , path="creditCardType" means the attribute in the Spring MVC model/command object, items="${creditCardTypes}" means all the populated credit card types will be available in "creditCardTypes" ModelAttribute. Thats it !!!