Use properties file to load select options (Drop down list) in spring form

This solution will show you how to place the country options in a properties file. The values will no longer be hard coded in the Java code.
1. Create a properties file to hold the countries. It will be a name value pair.  Country code is name. Country name is the value.
New text file:  WEB-INF/countries.properties
Note the location of the properties file is very important. It must be stored in WEB-INF/countries.properties
2. Update header section for Spring config file
We are going to use a new set of Spring tags for <util>. As a result, you need to update the header information in the Spring config file.
File: spring-mvc-dmo-servlet.xml
Remove the previous header and add this.
3. Load the country options properties file in the Spring config file. Bean id: countryOptions
File: spring-mvc-dmo-servlet.xml
Add the following lines:
<util:properties id="countryOptions" location="classpath:../countries.properties" /> 
4. Inject the properties values into your Spring Controller: StudentController.java
5. Add the country options to the Spring MVC model. Attribute name: theCountryOptions
6. Update the JSP page, student-form.jsp, to use the new model attribute for the drop-down list: theCountryOptions
7. Remove all references to country option from your Student.java.  

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