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

Use @Initbinder in Spring MVC

Nginx

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

Different ways to Authenticate a Web Application

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

Why you should use BCrypt to hash passwords

CSRF Protection using Double Submitted Cookies

CSRF Protection using Synchronizer Tokens

Use of DispatcherServlet in Spring MVC Framework

Agile Methodology – Know the What and How?