How to populate radio buttons with items from Java class?
Here are the steps
1. Set up the data in your Student class
Add a new field
private LinkedHashMap<String, String> favoriteLanguageOptions;
In your constructor, populate the data
// populate favorite language options
favoriteLanguageOptions = new LinkedHashMap<>();
favoriteLanguageOptions = new LinkedHashMap<>();
// parameter order: value, display label
//
favoriteLanguageOptions.put("Java", "Java");
favoriteLanguageOptions.put("C#", "C#");
favoriteLanguageOptions.put("PHP", "PHP");
favoriteLanguageOptions.put("Ruby", "Ruby");
//
favoriteLanguageOptions.put("Java", "Java");
favoriteLanguageOptions.put("C#", "C#");
favoriteLanguageOptions.put("PHP", "PHP");
favoriteLanguageOptions.put("Ruby", "Ruby");
Add getter method
public LinkedHashMap<String, String> getFavoriteLanguageOptions() {
return favoriteLanguageOptions;
}
return favoriteLanguageOptions;
}
2. Reference the data in your form
Favorite Language:
<form:radiobuttons path="favoriteLanguage" items="${student.favoriteLanguageOptions}" />
<form:radiobuttons path="favoriteLanguage" items="${student.favoriteLanguageOptions}" />
Comments
Post a Comment