Spring - MVC Form Tag Library

Last Updated : 9 Jun, 2026

The Spring Form Tag Library (spring-form.tld) is a collection of JSP tags provided by Spring MVC for creating and processing web forms. These tags simplify form handling by automatically binding form fields to model objects.

  • Automatic form-to-model data binding.
  • Built-in validation support.
  • Reduces boilerplate code.
  • Provides tags for common form elements.

Configuration

To use Spring Form Tags in a JSP page, import the Spring Form Tag Library using the following directive:

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

  • prefix="form": Specifies the prefix used for Spring Form Tags.
  • uri: Identifies the Spring Form Tag Library provided by Spring MVC.

Common Spring Form Tags

The Spring Form Tag Library provides the following commonly used tags for creating and processing forms in Spring MVC applications:

Spring MVC <form:form> Tag

The <form:form> tag is used to create an HTML form and bind it to a model object in Spring MVC.

  • Supports automatic data binding between form fields and model attributes.
  • Handles form submission using the specified action and method.

Syntax:

<form:form action="submit" method="post" modelAttribute="user">

Spring MVC <form:input> Tag

The <form:input> tag is used to create an input field and bind it to a model property.

  • Supports automatic data binding with model attributes.
  • Can be used for text, email, date, and other input types.

Syntax:

<form:input path="name" />

Spring MVC <form:label> Tag

The <form:label> tag is used to create a label for a form field.

  • Improves form readability and accessibility.
  • Associates descriptive text with form elements.

Syntax:

<form:label path="name">Full Name</form:label>

Spring MVC <form:password> Tag

The <form:password> tag is used to create a password input field.

  • Hides entered characters for security.
  • Supports binding password values to model properties.

Syntax:

<form:password path="password" />

Spring MVC <form:checkbox> Tag

The <form:checkbox> tag is used to create a single checkbox and bind it to a model property.

  • Allows users to select or deselect an option.
  • Automatically handles checkbox value binding.

Syntax:

<form:checkbox path="job" value="Yes" />

Spring MVC <form:checkboxes> Tag

The <form:checkboxes> tag is used to generate multiple checkboxes from a collection of values.

  • Supports selecting multiple options.
  • Can populate values dynamically from a List, Array, or Map.

Syntax:

<form:checkboxes path="skill" items="${programmingSkills}" />

Spring MVC <form:radiobutton> Tag

The <form:radiobutton> tag is used to create a single radio button and bind it to a model property.

  • Allows selection of one option from multiple choices.
  • Multiple radio buttons share the same path with different values.

Syntax:

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

Spring MVC <form:radiobuttons> Tag

The <form:radiobuttons> tag is used to generate multiple radio button options from a collection.

  • Displays dynamic radio button options.
  • Allows selection of only one value from the available choices.

Syntax:

<form:radiobuttons path="years" items="${experienceYears}" />

Spring MVC <form:select> Tag

The <form:select> tag is used to create a dropdown list and bind the selected value to a model property.

  • Supports single-value selection from multiple options.
  • Can be used with <form:option> or <form:options> tags.

Syntax:

<form:select path="jobType" items="${jobType}" />

Spring MVC <form:option> Tag

The <form:option> tag is used to define a single option inside a dropdown list.

  • Represents an individual item in a select element.
  • Works as a child tag of <form:select>.

Syntax:

<form:option value="Intermediate" />

Spring MVC <form:options> Tag

The <form:options> tag is used to generate multiple dropdown options from a collection.

  • Supports dynamic option generation.
  • Can populate options from a List, Array, or Map.

Syntax:

<form:options items="${jobType}" />

Spring MVC <form:button> Tag

The <form:button> tag is used to create a button inside a Spring MVC form.

  • Commonly used for form submission.
  • Supports different button types through attributes.

Syntax:

<form:button>Submit</form:button>

Spring MVC <form:hidden> Tag

The <form:hidden> tag is used to create a hidden form field.

  • Stores values that should not be visible to users.
  • Sends hidden data to the server during form submission.

Syntax:

<form:hidden path="jobType" />

Spring MVC <form:errors> Tag

The <form:errors> tag is used to display validation errors for a form field.

  • Integrates with Spring MVC validation.
  • Displays error messages associated with a model property.

Syntax:

<form:errors path="name" cssStyle="color:red" />

Steps to Create Spring MVC Form Using Form Tags

Follow these steps to create a Spring MVC application using Spring Form Tags.

Spring MVC Application Form
Spring MVC Application Form

Step 1: Create a Maven Project

  • Open STS IDE.
  • Click File - New - Maven Project.
  • Select Create a simple project (Select archetype ) and click Next.

Then Enter the following details:

  • Group Id: com.gfg
  • Artifact Id: simple-calculator.geeksforgeeks.org
  • Packaging: war

Click Finish.

Below is the final project structure of the Spring MVC project after creating *.java and *.jsp files also.

Project Structure
Project Structure

Step 2: Add Required Dependencies

Add the following maven dependencies and plugin to your pom.xml file.

XML
<dependencies>

    <!-- Spring MVC -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.18</version>
    </dependency>

    <!-- Servlet API -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>

</dependencies>

Step 3: Create Bean Class (Welcome.java)

Create a Java Bean class named Welcome containing form properties along with getter and setter methods. Spring MVC uses this bean to bind form data automatically.

Java
package com.geek.app;

import javax.validation.constraints.Min;
import org.hibernate.validator.constraints.NotEmpty;

public class Welcome {
    
    @NotEmpty(message = "Please enter your name.")
    private String name;
  
    @Min(value= 6, message = "Minimum password length is 6 characters.")
    private String password;
    private String email;
    private String gender;
    private String education;
    private String company;
    private String job;
    private String jobType;
    private String years;
    private String skill;
    private String note;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getEducation() {
        return education;
    }
    public void setEducation(String education) {
        this.education = education;
    }
    public String getCompany() {
        return company;
    }
    public void setCompany(String company) {
        this.company = company;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    public String getJobType() {
        return jobType;
    }
    public void setJobType(String jobType) {
        this.jobType = jobType;
    }
    public String getYears() {
        return years;
    }
    public void setYears(String years) {
        this.years = years;
    }
    public String getSkill() {
        return skill;
    }
    public void setSkill(String skill) {
        this.skill = skill;
    }
    public String getNote() {
        return note;
    }
    public void setNote(String note) {
        this.note = note;
    }
    
}

Step 4: Create JSP Form Page (welcome.jsp)

Create welcome.jsp using Spring Form Tags. This page displays input fields, radio buttons, checkboxes, and a dropdown list that are automatically bound to the Welcome bean.

HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Welcome page</title>
</head>
<body>

    <h1>Welcome to GeeksforGeeks!</h1>
    <h3>Please enter your details</h3>

    <form:form action="submit" method="post" modelAttribute="welcome">
        <table>
            <tr>
                <td><form:label path="name">Full Name: </form:label></td>
                <td><form:input path="name" />
                <form:errors path="name" cssStyle="color:red"/></td>
            </tr>
            <tr>
                <td><form:label path="password">Set your Password: </form:label></td>
                <td><form:password path="password" />
                <form:errors path="password" cssStyle="color:red"/></td>
            </tr>
            <tr>
                <td><form:label path="email">E-Mail Address: </form:label></td>
                <td><form:input path="email" /></td>
            </tr>
            <tr>
                <td><form:label path="gender">Gender: </form:label></td>
                <td><form:radiobutton path="gender" value="Male" label="Male" />
                    <form:radiobutton path="gender" value="Female" label="Female" /></td>
            </tr>
            <tr>
                <td><form:label path="education">Highest Education: </form:label></td>
                <td><form:select path="education">
                        <form:option value="Intermediate"></form:option>
                        <form:option value="Graduation"></form:option>
                        <form:option value="Post-Graduation"></form:option>
                    </form:select></td>
            </tr>
            <tr>
                <td><form:label path="job">Looking for a job change?: </form:label></td>
                <td><form:checkbox path="job" value="Yes" /></td>
            </tr>
            <tr>
                <td><form:label path="company">Current company Name: </form:label></td>
                <td><form:input path="company" /></td>
            </tr>

            <tr>
                <td><form:label path="jobType">Job Type: </form:label></td>
                <td><form:select path="jobType" items="${jobType}">
                        </form:select></td>
            </tr>
            <tr>
                <td><form:label path="years">Years of experience: </form:label></td>
                <td><form:radiobuttons path="years" items="${experienceYears}"
                        delimiter="|" /></td>
            </tr>
            <tr>
                <td><form:label path="skill">Programming skills: </form:label></td>
                <td><form:checkboxes path="skill" items="${programmingSkills}" delimiter="|"/></td>
            </tr>
            <tr>
                <td><form:label path="note">Profile summary : </form:label></td>
                <td><form:textarea path="note" rows="4" cols="25"/></td>
            </tr>
            <tr>
                <td><form:button>Submit</form:button></td>
            </tr>
        </table>
    </form:form>

</body>
</html>

Step 5: Create Controller Class (WelcomeController.java)

Create a controller class to load the form page, provide checkbox and dropdown values, and process the submitted form data.

Java
package com.geek.app;

import java.util.Arrays;
import java.util.List;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class WelcomeController {

    @RequestMapping(value = "/")
    public String viewPage(Model model) {
        
        Welcome welcome = new Welcome();
        model.addAttribute("welcome", welcome);
        return "welcome";
    }
    
    @ModelAttribute("jobType")
    public List<String> jobType(){
        List<String> type = Arrays.asList("Permanent", "Contract Based");
        return type;
    }
    
    @ModelAttribute("programmingSkills")
    public List<String> programmingSkills(){
        List<String> skills = Arrays.asList("Java", "C++", "Python", "PHP", "JavaScript");
        return skills;
    }
    
    @ModelAttribute("experienceYears")
    public List<String> experienceYears(){
        List<String> years = Arrays.asList("1-3 Years", "3-5 years", "Above 5 Years");
        return years;
    }

    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public String submit(@Valid @ModelAttribute("welcome") Welcome welcome, BindingResult result) {
        
        if (result.hasErrors()) {
            return "welcome";
        }
        else {
            return "summary";
        }
    }
}

Step 6: Create Result Page (summary.jsp)

Create summary.jsp to display the values submitted by the user.

HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Summary page</title>
</head>
<body>

    <h3>Details Submitted!!</h3>

    <table>
        <tr>
            <td>Full Name:</td>
            <td>${welcome.name}</td>
        </tr>
        <tr>
            <td>Your Password:</td>
            <td>${welcome.password}</td>
        </tr>
        <tr>
            <td>E-Mail Address:</td>
            <td>${welcome.email}</td>
        </tr>
        <tr>
            <td>Gender:</td>
            <td>${welcome.gender}</td>
        </tr>
        <tr>
            <td>Highest Education:</td>
            <td>${welcome.education}</td>
        </tr>
        <tr>
            <td>Current company Name:</td>
            <td>${welcome.company}</td>
        </tr>
        <tr>
            <td>Looking for a job change?:</td>
            <td>${welcome.job}</td>
        </tr>
        <tr>
            <td>Job Type:</td>
            <td>${welcome.jobType}</td>
        </tr>
        <tr>
            <td>Years of experience:</td>
            <td>${welcome.years}</td>
        </tr>
        <tr>
            <td>Programming skills:</td>
            <td>${welcome.skill}</td>
        </tr>
        <tr>
            <td>Profile summary :</td>
            <td>${welcome.note}</td>
        </tr>
    </table>

</body>
</html>

Step 7: Run the Application

  • Right-click the project.
  • Select Run As → Run on Server.
  • Choose Apache Tomcat Server.
  • Click Finish.

After that, use the following url on Browser:

http://localhost:8080/app

Output:

Welcome Page
Welcome Page 

As we are validating the fields Full Name and Password, submit the form without entering the Full Name and Password like below.

Error Details
Error Details

This way we will be getting the error to input the correct values. Now, enter all the information and submit the form.

Input details
Input details

After click on submit button All the details entered are submitted and being displayed on the UI screen. This way, we can use the Spring form tags in the Spring tag library - spring-form.tld based on our project requirement.

Output
Output
Comment