1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

springのバリデーションの実装時のエラーについて

Posted at
HTTPステータス 400 - Bad Request
Type ステータスレポート

説明 The server cannot or will not process the request due to something 
that is perceived to be a client error (e.g., malformed request syntax, 
invalid request message framing, or deceptive request routing).

Apache Tomcat/8.5.32

結論から言うと『BindingResult』の順番間違い

リクエストを送ったPOSTコントローラーでの引数に『BindingResult』オブジェクトを使用していたが、その順番を間違えていたから。

@RequestMapping(value = "/hoge", method = RequestMethod.POST)
public String hogeHoge(@Valid @ModelAttribute EmployeeListForm form, BindingResult result, Model model) {
    if (result.hasErrors()) {
        model.addAttribute("title", "エラー");
        model.addAttribute("message", "以下のエラーを解消してください");
    } else {
        EmployeeDto dto = new EmployeeDto();
        BeanUtils.copyProperties(form, dto);
        employeeList.add(dto);
        model.addAttribute("title", "社員一覧");
        model.addAttribute("message", form.getName() + "を登録しました。");
        model.addAttribute("employeeListForm", new EmployeeListForm());
    }
    model.addAttribute("employeeList", employeeList);
    return "hoge";
}

このように引数には
hogeHoge(formオブジェクト, BindingResult, Model)
の順番にしないといけない

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?