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?

【個人学習】boot_two_saleのProductController.javaを読んでSpring MVCのコントローラー実装とリファクタリングポイントを理解した

0
Posted at

対象ファイル

  • src/main/java/co/kr/sale/ProductController.java

はじめに

今回は boot_two_sale プロジェクトのコントローラー層である ProductController.java を分析した。
商品関連の Web リクエストを受け付け、データ処理を行い、ビューへ受け渡す Spring MVC の中核となるコントローラーの動作構造について整理してみた。

概要 / 基本情報 / 詳細な解説テーブル

co.kr.sale.ProductController は、商品関連の Web リクエストを受信し、商品一覧の検索、登録、詳細조회などのビジネスロジック処理とデータベース連携を制御する Spring MVC コントローラークラスである。

  • ファイルパス: src/main/java/co/kr/sale/ProductController.java
  • 種別: Class (Spring MVC Controller)
  • 主要アノテーション: @Controller, @Autowired, @RequestMapping
  • 依存関係: co.kr.sale.mapper.ProductDaoMapper, co.kr.sale.dto.ProductDto

処理フローの詳細 (5段階 Web 動作フロー)

  1. Request (リクエスト受信): クライアントが各機能の URL(例: /productList, /productSave 等)へ HTTP リクエストを送信。
  2. Controller (リクエストマッピング): Spring の DispatcherServlet が @RequestMapping にマッピングされた ProductController の各ハンドラーメソッドを呼び出し。
  3. DAO / Service (データ取得): @Autowired で注入された ProductDaoMapper オブジェクトの各メソッドを実行し、Oracle DB からデータの取得や登録(int result)処理を実施。
  4. DTO & Model (データ保持): 取得した List<ProductDto> や ProductDto、処理結果メッセージ等を HttpServletRequest 領域に属性 (Attribute) として設定。
  5. View Response (画面返却): 論理 View 名(例: "product/product_list", "common_alert" 等)を返却し、対応する JSP 画面をレンダリングして返却。

気になった部分・改善ポイント (Refactoring Notes)

  • Spring Model オブジェクトの活用推奨: 現在 HttpServletRequest を直接引数で受け取り setAttribute() を使用している。Spring MVC の標準スタイルである org.springframework.ui.Model オブジェクト (model.addAttribute()) を使用する設計へのリファクタリングが推奨される。
  • @RequestParam および @ModelAttribute の適用: request.getParameter() による手動のパラメータ取得や null チェック、個別の DTO 生成の代わりに、Spring のバインディングアノテーションを活かしてコードを簡潔化する必要がある。
  • Service 層の分離検討: Controller から MyBatis Mapper (ProductDaoMapper) を直接注入して呼び出している。トランザクション管理やビジネスロジックの分離のため、中間に Service 層 (ProductService) を配置する構造への改善が望ましい。

構成要素のまとめ

メソッド名 戻り値の型 パラメータ 役割・説明
productList() String HttpServletRequest request 検索条件(t_select, t_search)をもとに商品一覧を DB から取得して Request 領域に格納し、一覧 View パスを返却
productWriteForm() String なし (void) 商品登録フォーム画面の View パスを返却
productSave() String HttpServletRequest request フォームから送信されたパラメータを ProductDto にバインドして登録処理を実行し、結果メッセージを設定して共通アラート画面を返却
productView() String HttpServletRequest request 指定された商品コードをもとに詳細情報を DB から取得して Request 領域に格納し、詳細 View パスを返却

今回学んだこと

  • boot_two_sale プロジェクトにおけるコントローラー層がどのようにリクエストを受け取り、DAO と連携して JSP にデータを渡しているのかの一連の流れを把握できた。
  • レガシーなサーブレット的記述(HttpServletRequest の直接操作)から Spring MVC の標準的な機能(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?