Spring MVC Tutorial
Core Spring MVC
Spring MVC - Annotation
Spring MVC - Form Handling
Spring MVC with JSTL
Spring MVC with REST API
Spring MVC with Database
Data binding in Spring MVC allows developers to bind form inputs to Java objects automatically. This is particularly useful when dealing with form submissions in web applications.
Let's consider an example where we have a simple User registration form, and we want to bind the form input fields to a User
Java object.
First, let's define the User
model.
public class User { private String username; private String password; private String email; // standard getters and setters }
In the controller, we'll handle two requests:
package com.example.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/register") public class RegistrationController { @GetMapping public String showRegistrationForm(Model model) { model.addAttribute("user", new User()); return "registrationForm"; } @PostMapping public String processRegistration(@ModelAttribute("user") User user) { // Process the registration (e.g., save the user to the database) // Redirect to some page after registration, for now, let's just redirect to the same form return "redirect:/register"; } }
Let's create a Thymeleaf view for our registration form (src/main/resources/templates/registrationForm.html
):
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Registration</title> </head> <body> <form action="#" th:action="@{/register}" th:object="${user}" method="post"> <label for="username">Username:</label> <input type="text" th:field="*{username}" id="username" /> <label for="password">Password:</label> <input type="password" th:field="*{password}" id="password" /> <label for="email">Email:</label> <input type="email" th:field="*{email}" id="email" /> <input type="submit" value="Register" /> </form> </body> </html>
The th:field
attribute is used to bind the form fields to the properties of the User
object.
Run your Spring Boot application and navigate to http://localhost:8080/register
. Submitting the form will bind the input data to a User
object and process it in the processRegistration
method of the RegistrationController
.
Note: This is a very basic example. In a real-world scenario, you'd probably include data validation, error handling, and other features. Spring MVC offers the @Valid
annotation along with the BindingResult
object to help with validation and error handling when performing data binding.
Spring MVC form data binding:
commandName
attribute in the <form>
tag. For example:<form:form modelAttribute="user" method="post" action="processForm"> <form:input path="username" /> <form:input path="password" /> <input type="submit" value="Submit" /> </form:form>In the corresponding controller:
@PostMapping("/processForm") public String processForm(@ModelAttribute("user") User user) { // Process user data return "result"; }
How to use @ModelAttribute
in Spring MVC:
@ModelAttribute
annotation in Spring MVC is used to bind method parameters to a model attribute. For example:@GetMapping("/showForm") public String showForm(@ModelAttribute("user") User user) { // Prepare data or perform any necessary actions return "input-form"; }
Example of data binding in Spring MVC:
User
class with username
and password
properties. Bind form data to this class in your controller method using @ModelAttribute
and process the submitted data.@Controller @RequestMapping("/user") public class UserController { @GetMapping("/showForm") public String showForm(@ModelAttribute("user") User user) { return "user-form"; } @PostMapping("/processForm") public String processForm(@ModelAttribute("user") User user) { // Process user data return "result"; } }
<!-- user-form.jsp --> <form:form modelAttribute="user" method="post" action="/user/processForm"> <form:input path="username" /> <form:input path="password" /> <input type="submit" value="Submit" /> </form:form>