Spring - MVC Hidden Field

Last Updated : 12 Jun, 2026

Spring MVC provides the <form:hidden> tag to pass hidden data from the form to the server without displaying it on the user interface. It is mainly used to send non-user input values such as IDs, tokens, or database references along with form submission.

  • Used to send hidden data like IDs or metadata.
  • Not visible on UI but submitted with form data.
  • Automatically binds value to Java bean properties.

Syntax

<form:hidden path="eid"/>

  • path: Binds hidden field to a model property.
  • value (optional): Used to set a fixed value for the hidden field.

Attributes in <form:hidden> Tag

The <form:hidden> tag is used to send hidden data to the server without displaying it on the UI. It mainly supports a few core attributes for binding and configuration.

Attribute NameDescription
pathBinds the hidden field value to a model property.
valueSpecifies a fixed value for the hidden field (optional).
idAssigns a unique ID to the hidden input element.
htmlEscapeEnables or disables HTML escaping of the rendered value.

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": Defines the prefix used to access Spring form tags in JSP (e.g., <form:form>, <form:input>).
  • uri: Specifies the Spring Form Tag Library provided by Spring MVC for form binding and processing.

Steps to Implementation of Hidden Field in Spring MVC Application

Follow these steps to create a Spring MVC application using <form:hidden> tag.

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: SpringMVCApplication
  • 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 Dependencies (pom.xml)

.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: Model Class (Employee.java)

This class stores user input and hidden field data using getter and setter methods.

Java
package com.geeksforgeeks.app;

public class Employee {
    
    private String ename;
    private String eid;

    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public String getEid() {
        return eid;
    }
    public void setEid(String eid) {
        this.eid = eid;
    }
    
}

Step 4: Controller Class (EmployeeController.java)

Controller handles request mapping and processes form data.

Java
package com.geeksforgeeks.app;

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 EmployeeController {
    
    @RequestMapping(value = "/")
    public String viewHome(Model model) {

        Employee emp = new Employee();
        model.addAttribute("emp", emp);
        return "employee";
    }

    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public String submit(@ModelAttribute("emp") Employee emp) {
        return "summary";
    }

}

Step 5: Input Page (employee.jsp)

This page takes user input and sends hidden data using <form:hidden> tag.

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>
    <h2>Welcome to GeeksforGeeks!!</h2>

    <form:form action="submit" method="post" modelAttribute="emp">
        <form:label path="ename">Employee Name: </form:label>
        <form:input path="ename" />
        <form:hidden path="eid" value="1002" />
        <form:button>Submit</form:button>
    </form:form>

</body>
</html>

Step 6: Output Page (summary.jsp)

This page displays submitted data after processing.

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>Your Details</h3>

    <table>
        <tr>
            <td>Employee Name:</td>
            <td>${emp.ename}</td>
        </tr>
        <tr>
            <td>Employee ID:</td>
            <td>${emp.eid}</td>
        </tr>
    </table>

</body>
</html>

Step 7: Run Your 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:

On this page, we are taking 'Employee Name' as input from the user. The hidden field which we used in the form will not be displayed on the screen and that value will be set at the backend when the above form is submitted by the user.

Welcome Page
Welcome Page

If we want to see the value of the hidden field on the browser side, we can see that using Developer tools. press F12 to open the developer tools like below.

Developer tools
Developer tools

Now, enter the Name and click on submit.

Input
Input

Once, we click on submit, the form will be submitted with user-entered value and also the hidden field value provided in the code and generates the output like below.

Output
Output

Explanation: The user enters the employee name in the form while the hidden field automatically carries the employee ID to the backend. The controller processes both visible and hidden data and displays it on the result page.

Advantages

  • Helps pass important data (like IDs, tokens) to the server without showing it on the UI.
  • Automatically binds hidden values to model attributes in Spring MVC.
  • Useful for maintaining state between form requests (like update operations).

Limitations

  • Data is not secure because it can be viewed and modified using browser developer tools.
  • Not suitable for storing sensitive information like passwords or confidential data.
  • Debugging can be difficult since hidden values are not visible on the UI.
Comment