Quick revision
The short answer
A GET handler adds an empty SignupForm to the model and returns the view "signup". The POST handler takes @Valid @ModelAttribute("form") SignupForm form, BindingResult result and RedirectAttributes: if result.hasErrors() it returns "signup" so the form is redisplayed with errors, otherwise it saves the user, adds a flash message and returns "redirect:/welcome". BindingResult must come directly after the validated parameter, and post-redirect-get means answering a successful POST with a redirect, so a browser refresh repeats a harmless GET instead of resubmitting the form.
What to remember
In this answer, we wrote a sign-up form class with Bean Validation, a GET handler that adds an empty form and returns "signup", and a POST handler that binds and validates into a BindingResult placed directly after the form, redisplays "signup" when there are errors, and otherwise saves, adds a flash message and redirects to /welcome. We saw that conversion errors such as age=abc arrive as typeMismatch alongside validation errors, that a misplaced BindingResult turns errors into a 400 exception, and that post-redirect-get with flash attributes stops a refresh from resubmitting the form.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A GET handler adds an empty SignupForm to the model and returns the view "signup". The POST handler takes @Valid @ModelAttribute("form") SignupForm form, BindingResult result and RedirectAttributes: if result.hasErrors() it returns "signup" so the form is redisplayed with errors, otherwise it saves the user, adds a flash message and returns "redirect:/welcome". BindingResult must come directly after the validated parameter, and post-redirect-get means answering a successful POST with a redirect, so a browser refresh repeats a harmless GET instead of resubmitting the form.
Why this question is asked
Server-rendered forms are still everywhere: admin tools, internal apps, sign-up and settings pages. Getting them right means handling three outcomes cleanly: invalid input that must be shown back to the user with messages, input that cannot even be converted, like letters in an age field, and success, which must not be repeatable by pressing refresh. Interviewers ask for this to check that we know how Spring MVC binds, validates and redirects. In this answer, we will write the form class, the two controller methods, see how validation and type conversion errors reach BindingResult, understand why its position matters, explain post-redirect-get with flash attributes, and show the errors in Thymeleaf.
1The Form Class With Bean Validation
A form backing object is a plain class whose fields match the form inputs. Spring's data binding copies request parameters onto it by name, and Bean Validation annotations describe the rules. We use Integer rather than int for age, so an empty field becomes null and is reported by @NotNull instead of failing conversion.
2The Controller: Show, Validate, Redirect
The GET method puts an empty form in the model under the name "form" so the template can bind its fields. The POST method binds and validates, then chooses between redisplaying the form and redirecting.
- Bind parameters onto SignupFormconversion errors recorded as
typeMismatch - Run Bean Validationconstraint errors recorded on fields
- Check business rulesfor example email already taken
- result.hasErrors()?Yesreturn "signup": redisplay with errorsNosave the user
- Add a flash messagesurvives one redirect
- Return "redirect:/welcome"browser makes a new GET
6 steps in order. Play them, or step through them one at a time.
Notice that when we return "signup" after errors, the form object, with the values the user typed, is still in the model under "form", so the page shows their input rather than empty fields. The rejectValue call shows how to add a business rule error, such as a taken email, to the same BindingResult so it is displayed like any other field error.
3Conversion Errors and Validation Errors in One Place
Two kinds of problem end up in BindingResult. The first is conversion: the request parameter age=abc cannot become an Integer. Spring does not throw; it records a field error with the code typeMismatch and leaves age as null. The second is validation: once binding is done, @Valid runs the Bean Validation rules, and each failed constraint becomes a field error with the annotation's message.
The default message for typeMismatch is technical, mentioning the conversion failure. We replace it with a friendly one in messages.properties, which Spring Boot loads automatically, using the error code for the field.
4Why BindingResult Must Come Directly After the Form
Spring associates a BindingResult or Errors parameter with the model attribute declared immediately before it. If it is there, Spring puts the errors into it and calls our method, letting us decide what to do. If it is missing, or placed elsewhere, Spring has nowhere to put the errors and throws instead.
For a @ModelAttribute with @Valid, recent Spring versions throw MethodArgumentNotValidException, which extends BindException; older versions threw BindException itself. Either way the user gets a 400 error page instead of the form with messages.
5Post-Redirect-Get and Flash Attributes
If the POST handler rendered a welcome page directly, the browser's current page would be the result of a POST. Pressing refresh would make the browser offer to resend the form, and accepting would register the user twice, or charge a card twice in a checkout form. Post-redirect-get avoids this: the successful POST answers with a 302 redirect, the browser makes a fresh GET /welcome, and refreshing now repeats only that harmless GET.
The redirect creates a problem of its own: the model of the POST request is gone after the redirect, so how does the welcome page know what to say? Flash attributes solve it. ra.addFlashAttribute stores the value in the session just before the redirect, Spring adds it to the model of the very next request, and then removes it, so it appears once.
6 messages in order. Play them, or step through them one at a time.
Notice that the error path does not redirect. Returning the view directly with status 200 is what keeps the user's input and errors in the model. Only success redirects.
6Follow-up questions
How do you show field errors in a Thymeleaf template?
Bind the form with th:object="${form}" on the form element, and each input with th:field="*{name}", which sets the name, id and current value. Under each input, th:if="${#fields.hasErrors('name')}" checks for errors and th:errors="*{name}" prints the field's messages, while th:errorclass="is-invalid" adds a CSS class to the input when it has errors. For errors not tied to a field, #fields.hasGlobalErrors() and #fields.globalErrors() show them. Because the form object is still in the model after a failed POST, th:field also refills the user's input.
Why use @ModelAttribute("form") with an explicit name?
Without a name, Spring derives it from the class, signupForm, and the template must use that. Naming it explicitly makes the link between controller and template obvious and stable if the class is renamed. The name must match between the GET method, the POST method and th:object.
Does this form need CSRF protection?
Yes, when Spring Security is used it is enabled by default for form posts, and Thymeleaf's th:action automatically adds the hidden CSRF token field to the form. A plain action attribute would not, and the POST would be rejected with 403.
7Conclusion
In this answer, we wrote a sign-up form class with Bean Validation, a GET handler that adds an empty form and returns "signup", and a POST handler that binds and validates into a BindingResult placed directly after the form, redisplays "signup" when there are errors, and otherwise saves, adds a flash message and redirects to /welcome. We saw that conversion errors such as age=abc arrive as typeMismatch alongside validation errors, that a misplaced BindingResult turns errors into a 400 exception, and that post-redirect-get with flash attributes stops a refresh from resubmitting the form.
Open with
I'd have a SignupForm class with @NotBlank on name, @NotBlank and @Email on email, and @NotNull, @Min(18) and @Max on age, using Integer so an empty field is null rather than a conversion failure.
Then, if they want more
The GET handler adds new SignupForm to the model as form and returns the signup view. The POST handler takes @Valid @ModelAttribute("form") SignupForm, then BindingResult, then RedirectAttributes. If result.hasErrors(), I return signup, so the same template shows the user's input and the messages. If it's fine, I save, call addFlashAttribute with a welcome message, and return redirect:/welcome.
I can also add business errors like a taken email with rejectValue. Two details matter. BindingResult must be directly after the validated parameter, otherwise Spring has nowhere to put the errors and throws, MethodArgumentNotValidException in current versions, BindException before, and the user gets a 400 instead of the form.
And type conversion errors, like age=abc, don't throw; they're recorded in BindingResult with the code typeMismatch, and I can give them a friendly message in messages.properties. The post-redirect-get pattern is the reason for the redirect: if I rendered the success page from the POST, refresh would resubmit the form and register twice.
With a 302 to /welcome, refresh just repeats a GET. Flash attributes carry the message across that redirect once. In Thymeleaf I'd use th:object, th:field and th:errors to show the field errors.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a Spring MVC controller for a server-rendered sign-up form with fields name, email...": https://scrivio-interview-library.netlify.app/spring-boot/mvc-architecture/d1fc29c15f/