Spring MVC Form – Drop-Down List

Facebooktwitterredditpinterestlinkedinmailby feather

We have already discussed the Spring MVC form tags available in the form tag library. Form tags are used to develop web applications easily. In this tutorial, we are going to discuss the following tags:

<form:select> – It represents the HTML<select> tag, which presents a list of items. It also supports the nested <form:option> and <form:options> tag.

Below is given a code snippet which shows how to use the <form:select> tag:

The option tag is always defined inside the select tag.

<form:option> – It represents the HTML option tag, which presents a single option. E.g., one option tag represents a single option of the drop-down list.   

Below is given a code snippet which shows how to use the <form:option> tag:

<form:options> – It represents the HTML options tag, which presents a list of option tags. E.g., an options tag contains a list of option tags.

Below is given a code snippet which shows how to use the <form:options> tag:

Let’s understand the concept of <form:select> and <form:option> tags with the help of an example.

Example of MVC Form Drop-down list

Here, we are going to create an example of a drop-drop list. Below are the steps to create an example of a drop-down list:

  • Create a request page

In this step, we are going to create a request page named index.jsp.

index.jsp

  • Create the Model class

In this step, we are going to create a Model class named Student.java, which stores the data of the drop-down list.

Student.java

  • Create the Controller class

In this step, we are going to create a Controller named StudentController.java, which returns the JSP pages.

StudentController.java

  • Add the entry of Controller in the web.xml

In this step, we are going to add the entry of the Controller inside the web.xml file.

web.xml

  • Define the Model in another XML file.

In this step, we are going to define the Model in another XML file (spring-servlet.xml).

spring-servlet.xml

  • Create all the view pages

In this step, we are going to create all the view pages (JSP page).

studentform.jsp

student-data.jsp

Output

Spring MVC Form – Drop-Down List
Spring MVC Form – Drop-Down List 1
Spring MVC Form – Drop-Down List 2
Spring MVC Form – Drop-Down List 3

Read the list from a Java Class

In the above example, the web form read the values of the drop-down list from the studentform.jsp page. Now, we are going to create an example of reading the list data from a Java class (Bean).

Here, we are taking the previous example for modifications. The modifications are done in the following classes:

  • Student.java

In the above code snippet (Student.java), we have provided the drop-down list values. Now, the web form will read the list from the above Java class.

  • studentform.jsp

In the above code snippet (studentform.jsp), we have removed the drop-down list values from the above form page.

Output

Spring MVC Form – Drop-Down List 4
Spring MVC Form – Drop-Down List 5
Spring MVC Form – Drop-Down List 6
Spring MVC Form – Drop-Down List 7

In the above example, we have read the drop-down list values from a Java class. Similarly, we can read any data values like checkbox, radio-button, etc., from a Java class.

Facebooktwitterredditpinterestlinkedinmailby feather