Spring mvc select tag can be used in multiple ways. We will see some of the ways in which we can use spring mvc select tag as a dropdown or a multiple select list.
1. Spring MVC select tag as dropdown
We can use <form:option> or <form:options>
<form:option> tag is generally used when individual items are to be added.
1 2 3 4 5 6 7 |
<form:select path="designation"> <form:option value="none" label="Select"/> <form:option value="Junior Developer" label="Junior Developer"/> <form:option value="Developer" label="Developer"/> <form:option value="Senior Developer" label="Senior Developer"/> <form:option value="Manager" label="Manager"/> </form:select> |
<form:options> tag is generally used when a list is to be passed to the select tag.
1 2 3 |
<form:select path="department"> <form:options items="${availableDepartments}" /> </form:select> |
2. Spring MVC select tag as multiple select list box
For using select tag as a multi select list , we need to add multiple=”true”
1 2 3 |
<form:select path="hobbies" multiple="true"> <form:options items="${availableHobbies}"/> </form:select> |
The multiple=”true” is a optional attribute. You can get more details on the other attributes that can be used for
at the Spring Form TLD
Complete Example with Code
1. Create a Model Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package com.kscodes.sampleproject.model; import java.util.List; public class Employee { private String designation; private String department; private List<String> hobbies; public String getDesignation() { return designation; } public void setDesignation(String designation) { this.designation = designation; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public List<String> getHobbies() { return hobbies; } public void setHobbies(List<String> hobbies) { this.hobbies = hobbies; } } |
2. Create a Controller
In this example Controller is used to create the data required to show in the dropdown
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package com.kscodes.sampleproject.controller; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.kscodes.sampleproject.model.Employee; @Controller @RequestMapping("/employee") public class EmployeeController { @RequestMapping(method = RequestMethod.GET) public ModelAndView showEmployeeForm() { Employee employee = new Employee(); // Add the command object to the modelview ModelAndView mv = new ModelAndView("employee"); mv.addObject("employee", employee); //Add dynamic data to Departments List<String> availableDepartments = new ArrayList<String>(); availableDepartments.add("Human Resources"); availableDepartments.add("Finance"); availableDepartments.add("Admin"); availableDepartments.add("Quality Assurance"); availableDepartments.add("Products"); mv.addObject("availableDepartments", availableDepartments); // Add dynamic data to the Hobbies list List<String> availableHobbies = new ArrayList<String>(); availableHobbies.add("Reading"); availableHobbies.add("Dancing"); availableHobbies.add("Singing"); availableHobbies.add("Doing Nothing"); mv.addObject("availableHobbies", availableHobbies); return mv; } @RequestMapping(method = RequestMethod.POST) public String submitForm(Model model, Employee employee, BindingResult result) { model.addAttribute("employee", employee); return "success"; } } |
3. Create a View to show the form
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring MVC - Employee</title> </head> <body> <h2>Employee Details</h2> <form:form method="post" commandName="employee"> <table> <tr> <td><form:label path="designation">Designation </form:label></td> <td> <form:select path="designation"> <form:option value="none" label="Select"/> <form:option value="Junior Developer" label="Junior Developer"/> <form:option value="Developer" label="Developer"/> <form:option value="Senior Developer" label="Senior Developer"/> <form:option value="Manager" label="Manager"/> </form:select> </td> </tr> <tr> <td><form:label path="department">Department</form:label></td> <td> <form:select path="department"> <form:options items="${availableDepartments}" /> </form:select> </td> </tr> <tr> <td><form:label path="hobbies">Hobbies</form:label></td> <td> <form:select path="hobbies" multiple="true"> <form:options items="${availableHobbies}"/> </form:select> </td> </tr> <tr> <td colspan="2"><input type="submit" value="Submit" /></td> </tr> </table> </form:form> </body> </html> |
4. Create a success page to show the data selected on form
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <title>Spring MVC - Employee</title> </head> <body> <h2>Employee Data</h2> <table> <tr style="height:75px"> <td>Selected Designation - </td> <td>${employee.designation}</td> </tr> <tr style="height:75px"> <td>Selected Department - </td> <td>${employee.department}</td> </tr> <tr style="height:75px"> <td>Selected Hobbies - </td> <td> <c:forEach var="hobby" items="${employee.hobbies}"> ${hobby} <br /> </c:forEach> </td> </tr> </table> </body> </html> |
Execution of Code
After selecting some values and clicking the submit button, a success page will be displayed