Spring MVC Dynamic Dropdown List

Hey guys in this post, you will learn about populating the dropdown values from java model in Spring MVC with easy to understand example

Read More:

Read More:


  • Spring MVC dropdowns are represented by <form:select>
  • To read the options from Java object we will use <form:options />, it takes items as attribute which contains the java object property name
<form:select path="country">
 <form:options items="${employee.propertyName}" />

<spring:select> is the Spring MVC form tag. path attribute is used to bind the textbox value to the bean property using setter methods.

Complete Example

Lets create a step by step maven project to understand the Spring MVC

Create a maven project

First you need to create a maven webapp project. You can check the below post to create new maven webapp project –

  • Create a maven webapp project

Add Maven Dependencies

Open pom.xml and add the following maven dependencies

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<name>springmvc Maven Webapp</name>


Configure Spring MVC App

Create AppConfig.java class inside in.bushansirgur.springmvc.config package and add the following content

package in.bushansirgur.springmvc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@ComponentScan(basePackages = "in.bushansirgur.springmvc")
public class AppConfig {
	//define the bean for view resolver
	public ViewResolver viewResolver() {
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		return viewResolver;

Configure Dispatcher Servlet

Create MySpringMvcDispatcherServletInitializer.java inside in.bushansirgur.springmvc.config package and add the following content

package in.bushansirgur.springmvc.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class MySpringMvcDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	protected Class<?>[] getRootConfigClasses() {
		return null;

	protected Class<?>[] getServletConfigClasses() {
		return new Class[] {AppConfig.class};

	protected String[] getServletMappings() {
		return new String[] {"/"};


Create a Java model

Inside src/main/java create a package in.bushansirgur.springmvc.model, under this create Employee.java and add the following content

package in.bushansirgur.springmvc.model;

import java.util.LinkedHashMap;

public class Employee {
	private String firstName;
	private String lastName;
	private String country;
	private LinkedHashMap<String, String> countryOptions;

	public Employee() {
		countryOptions = new LinkedHashMap<String, String>();
		countryOptions.put("IND", "India");
		countryOptions.put("AUS", "Australia");
		countryOptions.put("GER", "Germany");
		countryOptions.put("CAN", "Canada");
		countryOptions.put("SAF", "South Africa");
	public String getFirstName() {
		return firstName;

	public void setFirstName(String firstName) {
		this.firstName = firstName;

	public String getLastName() {
		return lastName;

	public void setLastName(String lastName) {
		this.lastName = lastName;

	public String getCountry() {
		return country;

	public void setCountry(String country) {
		this.country = country;

	public LinkedHashMap<String, String> getCountryOptions() {
		return countryOptions;

	public void setCountryOptions(LinkedHashMap<String, String> countryOptions) {
		this.countryOptions = countryOptions;

Create a Controller

Inside src/main/java create a package in.bushansirgur.springmvc.controller, under this create HomeController.java and add the following content

package in.bushansirgur.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import in.bushansirgur.springmvc.model.Employee;

public class HomeController {
	@RequestMapping(value = "/")
	public String showHomePage(Model theModel) {
		theModel.addAttribute("employee", new Employee());
		return "home-page";
	@RequestMapping(value = "/processForm")
	public String showDashboardPage(@ModelAttribute("employee") Employee theEmployee, Model theModel) {
		theModel.addAttribute("employee", theEmployee);
		return "dashboard";

@ModelAttribute annotation will bind the form values to the Java object.

Create a view template

Create home-page.jsp inside webapp/WEB-INF/view folder and add the following content

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
	<h1>Read the textbox value</h1>
	<form:form action="processForm" modelAttribute="employee">
		First Name:<form:input path="firstName" /><br/><br/>
		Last Name: <form:input path="lastName" /><br/><br/>
		Country: <form:select path="country">
			<form:options items="${employee.countryOptions}" />
		<button type="submit">Submit</button>

model attribute in the <form:form> tag takes the name of the model which we added to the Model object in handler method.

Create dashboard.jsp inside webapp/WEB-INF/view folder and add the following content

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
	<h1>Read the textbox value</h1>
	<p>Employee First name: ${employee.firstName}</p>
	<p>Employee Last name: ${employee.lastName}</p>
	<p>Employee country: ${employee.country}</p>

Run the App

Start the tomcat server, navigate to the URL localhost:8080/springmvc/

That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

This Post Has 2 Comments

  1. Krishnamurthy

    Hello Bushan,

    How are you. Hope you are doing good. Your explanation is too good, I am follower to your java related posts.
    Can i have complete source code for above Example. So that i can complete project structure. then it is very easy to understand.

    Thanks for understanding

    1. Bushan Sirgur

      Hello Krishnamurthy,

      Unfortunately i do not have the complete source code. i will try to upload the source to github. Give me sometime.

      Thanks and Regards,
      Bushan SC

Leave a Reply