Spring MVC Form Tags Examples

Below are some Spring MVC Form Tags Examples. These shows how to use form inputs, radiobutton, checkboxes and error handlers.

Consider the below class as our DTO:

public class User {

    private String username;
    private String password;
    private String[] genderArray = {"Male", "Female"};
    private String gender;
    private String[] divisionArray = {"IT", "Finance", "Administration", "Business"};
    private String division;
    private String[] languagesArray = {"Java", "C#", "C", "Python", "Ruby"};
    private String[] languages;

    public String[] getGenderArray() {
        return genderArray;

    public String[] getDivisionArray() {
        return divisionArray;

    public String[] getLanguagesArray() {
        return languagesArray;

    public String getUsername() {
        return username;

    public void setUsername(String username) {
        this.username = username;

    public String getPassword() {
        return password;

    public void setPassword(String password) {
        this.password = password;

    public String getGender() {
        return gender;

    public void setGender(String gender) {
        this.gender = gender;

    public String getDivision() {
        return division;

    public void setDivision(String division) {
        this.division = division;

    public String[] getLanguages() {
        return languages;

    public void setLanguages(String[] languages) {
        this.languages = languages;

In our controller method, we have added our user form as:

@RequestMapping(value = "/register", method = RequestMethod.GET)
public String registerUser(ModelMap modelMap) {
    modelMap.addAttribute("user_form", new User());
    return "register";

First, add the form taglib on top of your jsp file:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

Using Form Input

To add input type text for username field:

<form:input path="username" />

Using Form Password

To add input type password for password field:

<form:password path="password" />

Using Form Radio Buttons

There are two ways you can add radiobuttons in your form. First, if you have list of items for your radio buttons, you can just:

<form:radiobuttons path="gender" items="${user_form.genderArray}" />

this will get the gender array from user dto and populate it with radio buttons. The way is that you can manually create your radio button:

<form:radiobutton path="gender" value="Male" />
<form:radiobutton path="gender" value="Female" />

Using Form Select Dropdown Box

<form:select id="month" path="heldMonth">
    <form:option value="">Select Division</form:option>
    <form:options items="${user_form.divisionArray}" />

Using Form Checkbox

<form:checkboxes items="${user_form.languages}" path="languages" />

Adding Form Errors

Form errors are those error texts that are automatically added if there is a validation error in the form. To add form error, add the below code to your jsp where you want to show the error:

<form:errors path="username" />

The default element for error is span. To change this, add the attribute element with your preferred element. For example, instead of span, we want to use p element:

<form:errors path="username" element="p" />
Share this tutorial!