Spring MVC RadioButton Example
Spring MVC provides 2 tags <form:radiobutton> and <form:radiobuttons> to display radio button on form. In this post lets see the Spring MVC RadioButton example and difference between the 2 tags used.
1. <form:radiobutton>
This tag is used to display radiobuttons with hardcoded values
1 2 | <form:radiobutton path="gender" value="Male"/>Male <form:radiobutton path="gender" value="Female"/>Female |
2. <form:radiobuttons>
This tag is used to display radiobuttons with dynamic values.
1 | <form:radiobuttons path="paymentMode" items="${paymentModes}" /> |
Spring MVC RadioButton Example
Lets see the Spring MVC RadioButton and RadioButtons example.
Model
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 | package com.kscodes.sampleproject.model; public class Employee { private String firstName; private String lastName; private String gender; private String paymentMode; 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 getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getPaymentMode() { return paymentMode; } public void setPaymentMode(String paymentMode) { this.paymentMode = paymentMode; } } |
Controller
Use the controller to create a empty Model and also a dynamic list for the radiobuttons tag.
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 | 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.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.kscodes.sampleproject.model.Employee; @Controller public class EmployeeController { @RequestMapping(value = "/employee", method = RequestMethod.GET) public ModelAndView showEmployeeForm() { Employee employee = new Employee(); // Create Dynamic list for <form:radiotbuttons> List<String> paymentModes = new ArrayList<String>(); paymentModes.add("Checks"); paymentModes.add("Cash"); paymentModes.add("Bank Account"); paymentModes.add("Gift Cards"); paymentModes.add("Travellers Checks"); // Add the command object to the modelview ModelAndView mv = new ModelAndView("employee"); mv.addObject("employee", employee); // Add the payments modes to model for <form:radiotbuttons> mv.addObject("paymentModes", paymentModes); return mv; } @RequestMapping(value = "/employee", method = RequestMethod.POST) public String submitForm(Model model, Employee employee) { model.addAttribute("employee", employee); return "success"; } } |
View
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 | <%@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="firstName">First Name :</form:label></td> <td><form:input path="firstName" /></td> </tr> <tr> <td><form:label path="lastName">Last Name :</form:label></td> <td><form:input path="lastName" /></td> </tr> <tr> <td><form:label path="gender">gender :</form:label></td> <td> <form:radiobutton path="gender" value="male"/>Male <form:radiobutton path="gender" value="female"/>Female </td> </tr> <tr> <td><form:label path="paymentMode">Payment Modes :</form:label></td> <td><form:radiobuttons path="paymentMode" items="${paymentModes}" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="Submit" /></td> </tr> </table> </form:form> </body> </html> |