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.
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.
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.
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