In Spring MVC, form handling and validation are essential for managing user input effectively and ensuring data integrity.
Form Handling in Spring MVC
Form handling allows us to capture data from HTML forms and bind it to Java objects (command objects). This process helps map user input to a Java class, enabling easy handling of form submissions.
Steps to handle a form in Spring MVC
- Create a Form Object: Define a Java class with fields that represent the form data.
- Define a Controller Method: This method will handle the form submission and use the
@ModelAttribute
annotation to bind form data to the object. - Create a Form in HTML: Use standard HTML form tags, and ensure that the form fields have names matching the object properties.
Example
1 2 3 4 5 |
public class User { private String name; private String email; // getters and setters } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@Controller public class UserController { @GetMapping("/userForm") public String showForm(Model model) { model.addAttribute("user", new User()); return "userForm"; } @PostMapping("/submitForm") public String submitForm(@ModelAttribute("user") User user) { // handle form submission return "formSuccess"; } } |
1 2 3 4 5 6 7 8 9 |
<form action="/submitForm" method="post"> <label for="name">Name:</label> <input type="text" name="name" id="name" /> <label for="email">Email:</label> <input type="email" name="email" id="email" /> <button type="submit">Submit</button> </form> |
Form Validation in Spring MVC
To validate the form data, Spring MVC integrates with Java’s Bean Validation API (JSR 303) and Hibernate Validator. You can use annotations like @NotNull
, @Size
, @Email
, etc., on your form object fields to enforce constraints.
Steps for validation
- Add Validation Annotations: Annotate the fields in your form object class.
- Use
@Valid
in Controller: In your controller method, use@Valid
to trigger the validation andBindingResult
to capture validation errors.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import javax.validation.constraints.Email; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; public class User { @NotNull @Size(min = 1, message = "Name cannot be empty") private String name; @NotNull @Email(message = "Please provide a valid email address") private String email; // getters and setters } |
1 2 3 4 5 6 7 8 9 10 11 12 |
@Controller public class UserController { @PostMapping("/submitForm") public String submitForm(@Valid @ModelAttribute("user") User user, BindingResult result) { if (result.hasErrors()) { return "userForm"; // return to form page if there are validation errors } // handle form submission if valid return "formSuccess"; } } |
Displaying Validation Errors
Spring MVC allows you to easily display validation errors on the form. You can use BindingResult
to check for errors and display them in the JSP/HTML view.
Example in JSP
1 2 3 4 5 6 7 8 9 10 11 |
<form action="/submitForm" method="post"> <label for="name">Name:</label> <input type="text" name="name" id="name" value="${user.name}" /> <span style="color: red">${#fields.hasErrors('name') ? #fields.errors('name') : ''}</span> <label for="email">Email:</label> <input type="email" name="email" id="email" value="${user.email}" /> <span style="color: red">${#fields.hasErrors('email') ? #fields.errors('email') : ''}</span> <button type="submit">Submit</button> </form> |