Spring MVC manages form submissions by binding HTML form fields to a Java model object, then validating and processing that object in a controller method. This avoids manually parsing individual request parameters for every field.
Key Points: • @ModelAttribute binds all matching form fields onto a model object's properties automatically based on field names. • A @PostMapping method accepts the bound model object as a parameter to handle the submitted form. • @RequestParam can bind individual fields directly when a full model object isn't needed. • @Valid triggers bean validation (e.g. @NotBlank, @Size) on the bound object, and a following BindingResult parameter captures any validation errors without throwing an exception. • Checking bindingResult.hasErrors() lets the controller return the form view again with error messages instead of proceeding when validation fails.
Example: A registration form with fields for name and email maps onto a UserForm object; @Valid @ModelAttribute UserForm form combined with BindingResult lets the controller re-render the form with field-level errors if the email is invalid.
Code Example:
@PostMapping("/register")
public String register(@Valid @ModelAttribute UserForm form,
BindingResult result) {
if (result.hasErrors()) {
return "registerForm";
}
userService.save(form);
return "redirect:/success";
}Interview Tip: A concise interview answer is:
"I bind form data to a model object using @ModelAttribute on a @PostMapping method, letting Spring populate the object's fields automatically. For validation, I add @Valid before the model attribute and a BindingResult right after it, so I can check hasErrors() and re-render the form with messages instead of throwing an exception."