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?

必要なデータはformの中に、編集画面の登録・更新の切り替えはformの中のDBから取得したデータで行う

Posted at

springbootの開発をしていて、思ったことを書き留めておこう。

データの持ち運びはformで行うこと

何を言っているのかというと、DBから取得した値をresultなどの変数に入れて

model.addAttribute("result", result);

なんてしていると、画面で入力エラーなどが起こった場合に消えてしまい、そのたびに再取得する必要が出てくる。
当然、上記のようなことをしていると入力エラー時の処理を書くたびに意識しないといけないし、当然取得漏れなどから不具合につながりやすい。

formに入れておけば再取得の必要が無く、使いまわせる。


登録・更新の切り替えは、DBから取得したformの中のデータで行う

登録・更新を行う画面は編集画面として1つのhtmlで行うことが多いかと思うが、登録時と更新時で登録・更新ボタンの表示や処理を切り替える必要が出てくる。

springbootならthymeleafを使うことが多いと思うが、その時に上記のresultなどで判断していると、先述の入力エラー時等が起きた際に登録・更新の切り替えに難儀する。

そのためDBで取得した値をformに入れて、それを使うのが確実だろう。

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?