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<>();
        // parameter order: value, display label
        //
        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;
    }
2. Reference the data in your form
        Favorite Language:
        
        <form:radiobuttons path="favoriteLanguage" items="${student.favoriteLanguageOptions}"  />

Comments

Popular posts from this blog

Nginx

AWS Configuration For RDS(postgres),ElastiCache(Redis) with ElasticBean

Use @Initbinder in Spring MVC

CSRF Protection using Synchronizer Tokens

How to read Dates with Hibernate

CSRF Protection using Double Submitted Cookies

Add Logging Messages in Spring 5.1 - All Java Config Version

The TRUE difference between [] and {{}} bindings in Angular

Hibernate and Primary Keys