[JAVA] Spring validation was important in the order of Form and BindingResult

Introduction

Nice to meet you, this is NekoSarada1101. This is Qiita's first post. I am studying Java and Servlets at a vocational school. I'm currently studying the Spring framework on my own, and I'm going to post an article about an error that took a long time to resolve.

Main subject

I have defined the following controller to validate the input of the login screen of the web application developed for studying Spring.

@PostMapping("/auth")
public String postLogin(@ModelAttribute @Validated LoginForm form, Model model, BindingResult result) {
    if (result.hasErrors()) {
        return "login";
    }
    User user = userService.login(form.getId(), form.getPassword());
    model.addAttribute("user", user);

    return "redirect:/menu";
}

If there is no problem with the input, it will return to the next screen, and if there is, it will return to the login screen. When I send a request to this controller ...

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Apr 28 14:36:33 JST 2020
There was an unexpected error (type=Bad Request, status=400).

The above error was displayed on the screen.

Cause and solution

The cause was the order of the controller arguments. It seems that the BindingResult must be defined immediately after the Form object to be validated.

@PostMapping("/auth")
public String postLogin(@ModelAttribute @Validated LoginForm form, BindingResult result, Model model) {
    if (result.hasErrors()) {
        return "login";
    }
    User user = userService.login(form.getId(), form.getPassword());
    model.addAttribute("user", user);

    return "redirect:/menu";
}

It must be in the order of hoge (Form, BindingResult, Model) like this.

At the end

There is no error log on the console, and this is the first time an error has occurred due to the order of the arguments. I had no idea what the problem was, and it took me a long time to resolve it.

Also, as I wrote at the beginning, I'm not used to these outputs in the first post, so if you have any mistakes or advice, I would be grateful if you could kindly let me know.

Recommended Posts

Spring validation was important in the order of Form and BindingResult
[For beginners] DI ~ The basics of DI and DI in Spring ~
Order of processing in the program
Filter the result of BindingResult [Spring]
[Java] Get the dates of the past Monday and Sunday in order
Form that receives the value of the repeating item in Spring MVC
[Order method] Set the order of data in Rails
[Rails] Ranking and pagination in order of likes
Install the plugin in Eclipse
Use the Findbugs plugin in Eclipse
Spring validation was important in the order of Form and BindingResult
Order of processing in the program
Spring Autowired is written in the constructor
Console input in Java (understanding the mechanism)
[Order method] Set the order of data in Rails
View the Gradle task in the Spring Boot project
Specify the encoding of static resources in Spring Boot
Until the use of Spring Data and JPA Part 2
Until the use of Spring Data and JPA Part 1
Check the behavior of getOne, findById, and query methods in Spring Boot + Spring Data JPA
[Spring Data JPA] Can And condition be used in the automatically implemented method of delete?
Branch processing by the return value of RestTemplate and the status code of ResponseEntity in Spring Boot
Was done in the base year of the Java calendar week
Put the file in the properties of string in spring xml configuration
Contributed to Gradle and was named in the release notes
[Rails] Where to be careful in the description of validation
Create more Tabs and Fragments in the Fragment of BottomNavigationView
Spring validation and error code
When I was worried about static methods in java interface, I arrived in the order of name interpretation
Memo of JSUG Study Group 2018 Part 2-Efforts for working specifications in the Spring and API era-
In order not to confuse the understanding of getters and setters, [Do not use accessors for anything! ]
I want to know the Method of the Controller where the Exception was thrown in the ExceptionHandler of Spring Boot
Get a proxy instance of the component itself in Spring Boot
Let's consider the meaning of "stream" and "collect" in Java's Stream API.
Set "Fixed display of rows and columns" and "Validation rules" in rubyXL
Coexistence of Flyway in the embedded database (h2) of the development environment and the release database (SQL Server) with Spring Boot
[Spring Boot] List of validation rules that can be used in the property file for error messages
Order of route evaluation in Spark
Explanation of the order of rails routes
[Rails] Difference in behavior between delegate and has_many-through in the case of one-to-one-to-many
Determine the device type of smartphones, tablets, and PCs with Spring Mobile
Specify the order in which configuration files and classes are loaded in Java
The story of forgetting to close a file in Java and failing
Procedure to make the value of the property file visible in Spring Boot
Set the number of seconds for fast forward and rewind in ExoPlayer
Confirmation and refactoring of the flow from request to controller in [httpclient]
(Determine in 1 minute) About the proper use of empty ?, blank? And present?
This and that of the implementation of date judgment within the period in Java
How to change the maximum and maximum number of POST data in Spark
Calculate the percentage of "good", "normal", and "bad" in the questionnaire using SQL
Find the maximum and minimum of the five numbers you entered in Java
[Rails] The cause of not being able to post was in form_with
Displaying the three-dimensional structure of DNA and proteins in Ruby-in the case of GR.rb