In this tutorial you will learn about the Spring MVC form tags
In this tutorial you will learn about the Spring MVC form tagsSpring framework provides the form specific tags for designing a form. You can also use the simple html form tag also for designing the form. To use the form tag in your JSP page you need to import the Tag Library into your page as.
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
A Simple example is given below that illustrates all the form tags
myForm.jsp
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <h2 align="center">Spring Form Tags</h2> <form:form method="POST" name="myForm" commandName="myForm" action="#"> <table align="center" cellpadding="10" cellspacing="10" bgcolor="#CCFFCC"> <tr> <td><form:label path="hiddenField">Hidden Field</form:label></td> <td><form:hidden path="hiddenField"></form:hidden></td> </tr> <tr> <td><form:label path="textField">Text Field</form:label></td> <td><form:input path="textField"></form:input></td> </tr> <tr> <td><form:label path="password">Password</form:label></td> <td><form:input path="password"></form:input></td> </tr> <tr> <td><form:label path="radioButton">Radio Button</form:label></td> <td><form:radiobutton path="radioButton" value="M" label="M"></form:radiobutton> <form:radiobutton path="radioButton" value="F" label="F"></form:radiobutton></td> </tr> <tr> <td><form:label path="radioButton">Radio Buttons from List</form:label></td> <td><form:radiobuttons items="${countryList}" path="checkBox" itemLabel="name" itemValue="id"></form:radiobuttons></td> </tr> <tr> <td><form:label path="checkBox">Check Box</form:label></td> <td><form:checkbox path="checkBox" value="S" label="Student"></form:checkbox> <form:checkbox path="checkBox" value="E" label="Employee"></form:checkbox></td> </tr> <tr> <td><form:label path="checkBox">Check Boxes from List</form:label></td> <td><form:checkboxes items="${countryList}" path="checkBox" itemLabel="name" itemValue="id" /></td> </tr> <tr> <td><form:label path="select">Select Country</form:label></td> <td><form:select path="select"> <form:option value="0" label="Please Select"></form:option> <form:options items="${countryList}" itemValue="id" itemLabel="name" /> </form:select></td> </tr> <tr> <td><form:label path="textArea">Text Area</form:label></td> <td><form:textarea path="textArea" cols="35" rows="3"></form:textarea></td> </tr> <tr> <td><form:label path="file">File Upload</form:label></td> <td><form:input type="file" path="file"></form:input></td> </tr> <tr> <td align="right"><input type="submit" value="Submit Form"></input></td> <td align="center"><input type="reset" value="Clear Form"></input></td> </tr> </table> </form:form>
Bean class for the above form
SampleInterfaceImp.java
package net.roseindia.model; import java.util.List; import org.springframework.web.multipart.commons.CommonsMultipartFile; public class MyForm { private String textField; private String hiddenField; private String password; private String radioButton; private String select; private List countryList; private String textArea; private String checkBox; private CommonsMultipartFile file; public String getHiddenField() { return hiddenField; } public void setHiddenField(String hiddenField) { this.hiddenField = hiddenField; } public String getTextField() { return textField; } public void setTextField(String textField) { this.textField = textField; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getRadioButton() { return radioButton; } public void setRadioButton(String radioButton) { this.radioButton = radioButton; } public String getSelect() { return select; } public void setSelect(String select) { this.select = select; } public List getCountryList() { return countryList; } public void setCountryList(List countryList) { this.countryList = countryList; } public String getTextArea() { return textArea; } public void setTextArea(String textArea) { this.textArea = textArea; } public String getCheckBox() { return checkBox; } public void setCheckBox(String checkBox) { this.checkBox = checkBox; } public CommonsMultipartFile getFile() { return file; } public void setFile(CommonsMultipartFile file) { this.file = file; } }
Controller for the form
SampleInterfaceImp.java
package net.roseindia.controller; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.validation.Valid; import net.roseindia.model.Country; import net.roseindia.model.MyForm; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class MyFormController { @RequestMapping("/myForm.html") public String loadForm(Map model, @Valid MyForm myForm) { ListcountryList = new ArrayList (); countryList = loadCountry(); model.put("countryList", countryList); return "myForm"; } public List loadCountry() { List countryList = new ArrayList (); countryList.add(new Country(1, "India", "Description Of India")); countryList.add(new Country(2, "China", "Description Of China")); countryList.add(new Country(3, "England", "Description Of England")); countryList.add(new Country(4, "Srilanka", "Description Of Srilanka")); countryList.add(new Country(5, "Kenya", "Description Of Kenya")); return countryList; } }
The Country model class for displaying select, checkboxes and radiobutton groups
Country.java
package net.roseindia.model; public class Country { private int id; private String name; private String description; public Country() { } public Country(int id, String name, String description) { this.id = id; this.name = name; this.description = description; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }