0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ModelAttributeしてるのにThymeleafでないって言われた時

Last updated at Posted at 2025-04-21

@PostMappingでちゃんと@ModelAttribute/model.addAttributeしてるのに!!

それ、間違いです。GetMappingの役割がわかれば一瞬です(わからなかった側の人間直で2時間苦戦していました)

GetMappingの役割

1.ビューに初期データを渡すため

ここで@ModelAttributeを使うことで、GETリクエスト時にフォームオブジェクトをビューに渡すことができます。

2.フォームデータのバインディング

@ModelAttributeは、フォームから送信されたデータをオブジェクトにバインドする役割も持っています。@PostMappingでフォームを送信するとき、@ModelAttributeは送信されたデータを受け取って、フォームオブジェクトにマッピングします。

3.GETリクエストでフォームオブジェクトをモデルにセット

@GetMapping@ModelAttributeを使う理由は、フォームが初期表示される際に、空のまたはデフォルトの状態でフォームオブジェクトをビューに渡すためです。これにより、ユーザーが入力するフォームフィールドを提供できます。

というわけで

controller
    @GetMapping("/menu")
    public String view(@ModelAttribute("SearchForm") SearchForm form,
                       @ModelAttribute("InputTrackInfoForm") InputTrackInfoForm inputForm,
                       Model model) {

こんな感じにつかってあげましょう

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?