Spring - MVC Form Radio Button

Last Updated : 9 Jun, 2026

Spring MVC provides built-in support for creating and processing radio buttons in JSP pages through the Spring Form Tag Library. Using the <form:radiobutton> and <form:radiobuttons> tags, developers can easily bind selected values to Java objects without manually handling request parameters. This simplifies form development and improves code maintainability.

  • <form:radiobutton> is used to create a single radio button and bind it to a model property.
  • <form:radiobuttons> is used to create multiple radio button options from a collection of values.
  • Supports automatic data binding between form fields and Spring MVC model attributes, reducing manual coding.

Spring Form Tag Library

The Spring Form Tag Library (spring-form.tld) is a collection of JSP tags provided by Spring MVC for creating and processing web forms. It helps developers build forms more easily by supporting automatic data binding between form fields and model objects.

  • Provides form-related tags such as <form:form>, <form:input>, <form:password>, <form:radiobutton>, <form:radiobuttons>, and <form:select>.
  • Supports automatic data binding using the path attribute.
  • Integrates with Spring MVC validation and error handling.

To use the tag library, first import it into the JSP page:

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

Spring MVC <form:radiobutton> Tag

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

  • Selecting one option from multiple choices.
  • Yes/No type selections.
  • Gender selection.

Syntax:

<form:radiobutton path="propertyName" value="value" />

  • path: Model property to bind the selected value.
  • value: Value submitted when the radio button is selected.

Spring MVC <form:radiobuttons> Tag

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

  • Selecting one value from a list of options.
  • Displaying dynamic radio button options from a collection.

Syntax:

<form:radiobuttons path="propertyName" items="${itemsList}" />

  • path: Property in the model object.
  • items: Collection containing radio button values.

Steps to Implement Spring MVC Form Radio Button

Follow these steps to create a Spring MVC application using <form:radiobutton> and <form:radiobuttons> tags.

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.

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 (Choice.java)

Create a Java Bean class named Choice containing name, job, experience, and years properties along with getter and setter methods. Spring uses this bean to bind form data automatically.

Java
package com.geek.app;

public class Choice {

    private String name;
    private String job;
    private String experience;
    private String years;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public String getExperience() {
        return experience;
    }

    public void setExperience(String experience) {
        this.experience = experience;
    }

    public String getYears() {
        return years;
    }

    public void setYears(String years) {
        this.years = years;
    }
}

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

Create choice.jsp using Spring Form Tags. This page displays a text field and radio button options for job status, work experience, and years of experience.

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>

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

    <table>

        <tr>
            <td><form:label path="name">Enter your name:</form:label></td>
            <td><form:input path="name"/></td>
        </tr>

        <tr>
            <td><form:label path="job">Are you looking for a job now?</form:label></td>
            <td>
                <form:radiobutton path="job" value="Yes" label="Yes"/>
                <form:radiobutton path="job" value="No" label="No"/>
            </td>
        </tr>

        <tr>
            <td><form:label path="experience">Do you have work experience?</form:label></td>
            <td>
                <form:radiobutton path="experience" value="Yes" label="Yes"/>
                <form:radiobutton path="experience" value="No" label="No"/>
            </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:button>Submit</form:button></td>
        </tr>

    </table>

</form:form>

</body>
</html>

Step 5: Create Controller Class (ChoiceController.java)

Create a controller class to handle requests. It loads the form page, provides experience year options, and processes the submitted form data.

Java
package com.geek.app;

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

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

@Controller
public class ChoiceController {

    @RequestMapping(value = "/")
    public String viewPage(Model model) {

        Choice choice = new Choice();
        model.addAttribute("choice", choice);

        return "choice";
    }

    @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(
            @ModelAttribute("choice") Choice choice) {

        return "summary";
    }
}

Step 6: Create Result Page (summary.jsp)

Create summary.jsp to display the values submitted by the user. It shows the name, job status, work experience, and selected years of experience

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>Hello ${choice.name}!!</h3>

    <span>Looking for a Job: </span>
    <span>${choice.job}</span>

    <br>

    <span>Work Experience: </span>
    <span>${choice.experience}</span>

    <br>

    <span>Years of Experience: </span>
    <span>${choice.years}</span>

</body>
</html>

Step 7: Run Your Application

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

Output:

After that use the following URL to run your controller:

http://localhost:8080/app

Welcome page - choice.jsp

Scenario 1: Enter the name, select the option No for Do you have work experience? field as below,

Scenario 1: Input

As we selected option No for Do you have work experience? field, all the radiobuttons of How many years of experience you have? the field is disabled. Now, click on submit button to get the output.

Scenario 1: Output

Scenario 2: Now, select option yes for Do you have work experience? field.

Scenario 2: Input

As we selected option Yes for Do you have work experience? field, all the radiobuttons of How many years of experience you have? field is enabled. Select the desired option. Click on submit button to get the output.

Scenario 2: Output

Explanation: When the user opens /, the viewPage() method creates a Choice object and sends it to choice.jsp. The radio button options are displayed using <form:radiobutton> and <form:radiobuttons> tags. When the user submits the form to /submit (POST), Spring automatically binds the selected values to the Choice object using @ModelAttribute and returns summary.jsp to display the submitted details.

Comment