Spring MVC hidden tag provides a way to render HTML hidden field.
The following tag
1 |
<form:hidden path="employeeId" value="12345" /> |
is rendered into HTML hidden field as
1 |
<input id="employeeId" name="employeeId" value="12345" type="hidden"/> |
Example : Spring MVC Hidden Tag
In this example we will declare a field “employeeId” as hidden in the form and display its value on submit.
1. Model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.kscodes.sampleproject.model; public class Employee { private String firstName; private String lastName; private String employeeId; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmployeeId() { return employeeId; } public void setEmployeeId(String employeeId) { this.employeeId = employeeId; } } |
2. Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.kscodes.sampleproject.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.kscodes.sampleproject.model.Employee; @Controller public class EmployeeController { @RequestMapping(value = "/employee", method = RequestMethod.GET) public ModelAndView showEmployeeForm() { Employee employee = new Employee(); // Add the command object to the modelview ModelAndView mv = new ModelAndView("employee"); mv.addObject("employee", employee); return mv; } @RequestMapping(value = "/employee", method = RequestMethod.POST) public String submitForm(Model model, Employee employee) { model.addAttribute("employee", employee); return "success"; } } |
3. View
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring MVC - Employee</title> </head> <body> <h2>Employee Details</h2> <form:form method="post" commandName="employee"> <table> <tr> <td><form:label path="firstName">First Name :</form:label></td> <td><form:input path="firstName" /></td> </tr> <tr> <td><form:label path="lastName">Last Name :</form:label></td> <td><form:input path="lastName" /></td> </tr> <form:hidden path="employeeId" value="12345" /> <tr> <td colspan="2"><input type="submit" value="Submit" /></td> </tr> </table> </form:form> </body> </html> |