From 7b70861fd8024bf6f5164495e10345b894200bec Mon Sep 17 00:00:00 2001 From: w-yuki Date: Fri, 25 Sep 2026 16:36:18 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[add]=E3=83=97=E3=83=AB=E3=83=80=E3=82=A6?= =?UTF-8?q?=E3=83=B3=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=97=E3=81=BE=E3=81=97?= =?UTF-8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/controller/BookInsertController.java | 15 +++- .../com/example/demo/entity/BookInsert.java | 4 +- .../repository/StorageLocationRepository.java | 9 ++ src/main/resources/templates/bookInsert.html | 84 +++++++++---------- 4 files changed, 64 insertions(+), 48 deletions(-) create mode 100644 src/main/java/com/example/demo/repository/StorageLocationRepository.java diff --git a/src/main/java/com/example/demo/controller/BookInsertController.java b/src/main/java/com/example/demo/controller/BookInsertController.java index 6ed1d7b..c725330 100644 --- a/src/main/java/com/example/demo/controller/BookInsertController.java +++ b/src/main/java/com/example/demo/controller/BookInsertController.java @@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.PostMapping; import com.example.demo.entity.BookInsert; import com.example.demo.repository.BookCategoryRepository; +import com.example.demo.repository.StorageLocationRepository; import com.example.demo.repository.SubCategoryRepository; import com.example.demo.service.BookInsertService; @@ -21,13 +22,18 @@ private BookInsertService bookInsertService; @Autowired private BookCategoryRepository categoryRepository; + @Autowired private SubCategoryRepository subCategoryRepository; +@Autowired +private StorageLocationRepository storageLocationRepository; + @GetMapping("/bookInsert") public String bookInsert(Model model) { model.addAttribute("categoryList",categoryRepository.findAll()); model.addAttribute("subCategoryList",subCategoryRepository.findAll()); + model.addAttribute("locationList",storageLocationRepository.findAll()); model.addAttribute("bookInsert",new BookInsert()); return "bookInsert"; } @@ -37,6 +43,7 @@ private SubCategoryRepository subCategoryRepository; // 確認画面から戻る: POSTされた入力中の値がformに入ったまま入力画面へ @PostMapping("/bookInsertConfirm") public String bookInsertConfirm(BookInsert bookInsert,Model model) { + System.out.println(bookInsert); bookInsertService.save(bookInsert); model.addAttribute("bookInsert",bookInsert); return "bookInsertConfirm"; @@ -46,6 +53,7 @@ private SubCategoryRepository subCategoryRepository; public String bookInsertBack(BookInsert bookInsert,Model model) { model.addAttribute("categoryList",categoryRepository.findAll()); model.addAttribute("subCategoryList",new ArrayList<>()); + model.addAttribute("locationList",storageLocationRepository.findAll()); model.addAttribute("bookInsert",bookInsert); return "bookInsert"; } @@ -56,16 +64,21 @@ private SubCategoryRepository subCategoryRepository; model.addAttribute("categoryList",categoryRepository.findAll()); model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); + model.addAttribute("locationList",storageLocationRepository.findAll()); model.addAttribute("bookInsert", bookInsert); return "bookInsert"; } + } - //画面で選択したカテゴリbookInsert.getBookCategoryId()) + //画面で選択したカテゴリbookInsert.getBookCategoryId()に選択した結果をいれる + //subCategoryRepository.findBookCategory_BookCategoryId(bookInsert.getBookCategoryId(1))); + //「カテゴリIDが1のサブカテゴリを取ってくる」 + //model.addAttribute("subCategoryList",取得結果) diff --git a/src/main/java/com/example/demo/entity/BookInsert.java b/src/main/java/com/example/demo/entity/BookInsert.java index ffe074a..9305b62 100644 --- a/src/main/java/com/example/demo/entity/BookInsert.java +++ b/src/main/java/com/example/demo/entity/BookInsert.java @@ -40,7 +40,7 @@ public class BookInsert { private String publisher; @Column(name = "publication_date") - private String publicationDate; + private LocalDate publicationDate; @Column(name = "location_id") private Integer location; @@ -66,7 +66,7 @@ public class BookInsert { @Column(name = "book_approval") private Integer bookApproval; - @Transient + @Transient //DB保存無効 private Integer bookCategoryId; diff --git a/src/main/java/com/example/demo/repository/StorageLocationRepository.java b/src/main/java/com/example/demo/repository/StorageLocationRepository.java new file mode 100644 index 0000000..861a62a --- /dev/null +++ b/src/main/java/com/example/demo/repository/StorageLocationRepository.java @@ -0,0 +1,9 @@ +package com.example.demo.repository; + +import org.springframework.data.jpa.repository.JpaRepository; + +import com.example.demo.entity.StorageLocation; + +public interface StorageLocationRepository extends JpaRepository { + +} \ No newline at end of file diff --git a/src/main/resources/templates/bookInsert.html b/src/main/resources/templates/bookInsert.html index 467e577..059917e 100644 --- a/src/main/resources/templates/bookInsert.html +++ b/src/main/resources/templates/bookInsert.html @@ -8,17 +8,12 @@

新規図書登録入力

- -


- +

新しく図書を登録します。必要事項jを入力し、「登録」ボタンを押してください

+ ※は必須項目です +

-
- -




-
+


-
- +
+

-
+


+
- + +

-
+


- -
- + +
+

-
- -

- -
- -

- -
- +

+ +
+ +

+ +
+ +


@@ -103,7 +97,7 @@ --> - +
From 044210adc30850ce5f47b99a0e38ece91e607564 Mon Sep 17 00:00:00 2001 From: w-yuki Date: Fri, 25 Sep 2026 16:59:14 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[add]=E8=AA=A4=E5=AD=97=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/templates/bookInsert.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/templates/bookInsert.html b/src/main/resources/templates/bookInsert.html index 059917e..04a99d9 100644 --- a/src/main/resources/templates/bookInsert.html +++ b/src/main/resources/templates/bookInsert.html @@ -9,7 +9,7 @@

新規図書登録入力



-

新しく図書を登録します。必要事項jを入力し、「登録」ボタンを押してください

+

新しく図書を登録します。必要事項を入力し、「登録」ボタンを押してください

※は必須項目です

@@ -78,7 +78,7 @@

-
+


From 280331fdf6d55e5fc9a360f3883e34bf0acdf370 Mon Sep 17 00:00:00 2001 From: w-yuki Date: Mon, 28 Sep 2026 13:09:29 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[add]ISBN=E6=9D=A1=E4=BB=B6=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/controller/BookInsertController.java | 31 +++++++++++++++---- .../com/example/demo/entity/BookInsert.java | 3 ++ src/main/resources/templates/bookInsert.html | 6 ++-- .../templates/bookInsertConfirm.html | 2 +- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/example/demo/controller/BookInsertController.java b/src/main/java/com/example/demo/controller/BookInsertController.java index c725330..e3d5b2f 100644 --- a/src/main/java/com/example/demo/controller/BookInsertController.java +++ b/src/main/java/com/example/demo/controller/BookInsertController.java @@ -5,6 +5,8 @@ import java.util.ArrayList; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @@ -42,19 +44,37 @@ private StorageLocationRepository storageLocationRepository; // 確認画面から戻る: POSTされた入力中の値がformに入ったまま入力画面へ @PostMapping("/bookInsertConfirm") - public String bookInsertConfirm(BookInsert bookInsert,Model model) { + public String bookInsertConfirm(@Validated BookInsert bookInsert,BindingResult result,Model model) { + if(result.hasErrors()) { + model.addAttribute("categoryList",categoryRepository.findAll()); + if(bookInsert.getBookCategoryId() != null) { //nullじゃなかったら + model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); + //そのカテゴリに属するサブカテゴリを持ってくる + } else{ + model.addAttribute("subCategoryList",new ArrayList<>()); + } + model.addAttribute("locationList",storageLocationRepository.findAll()); + model.addAttribute("bookInsert", bookInsert); + return "bookInsert"; + + } System.out.println(bookInsert); bookInsertService.save(bookInsert); model.addAttribute("bookInsert",bookInsert); return "bookInsertConfirm"; } + //確認画面から「もどる」ボタンを押したら入力画面に戻る @PostMapping("/bookInsert") public String bookInsertBack(BookInsert bookInsert,Model model) { model.addAttribute("categoryList",categoryRepository.findAll()); - model.addAttribute("subCategoryList",new ArrayList<>()); - model.addAttribute("locationList",storageLocationRepository.findAll()); - model.addAttribute("bookInsert",bookInsert); + if(bookInsert.getBookCategoryId() != null) { + model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); + } else{ + model.addAttribute("subCategoryList",new ArrayList<>()); + } + model.addAttribute("locationList",storageLocationRepository.findAll()); + model.addAttribute("bookInsert",bookInsert); return "bookInsert"; } @@ -62,8 +82,7 @@ private StorageLocationRepository storageLocationRepository; public String changeCategory(BookInsert bookInsert,Model model) { //カテゴリプルダウンに表示するデータを取得 model.addAttribute("categoryList",categoryRepository.findAll()); - - model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); + model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); model.addAttribute("locationList",storageLocationRepository.findAll()); model.addAttribute("bookInsert", bookInsert); diff --git a/src/main/java/com/example/demo/entity/BookInsert.java b/src/main/java/com/example/demo/entity/BookInsert.java index 9305b62..be1fd47 100644 --- a/src/main/java/com/example/demo/entity/BookInsert.java +++ b/src/main/java/com/example/demo/entity/BookInsert.java @@ -8,6 +8,7 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; import jakarta.persistence.Transient; +import jakarta.validation.constraints.Pattern; import lombok.Data; @@ -27,6 +28,8 @@ public class BookInsert { @Column(name = "title") private String title; + @Pattern(regexp = "^(?=(?:.*-){4})[0-9-]{17}$", + message = "ISBNは半角数字(13文字)とハイフン(4つ)を含めて入力してください") @Column(name = "isbn") private String isbn; diff --git a/src/main/resources/templates/bookInsert.html b/src/main/resources/templates/bookInsert.html index 04a99d9..be4be18 100644 --- a/src/main/resources/templates/bookInsert.html +++ b/src/main/resources/templates/bookInsert.html @@ -26,7 +26,9 @@


- + +
+



@@ -69,7 +71,7 @@


- diff --git a/src/main/resources/templates/bookInsertConfirm.html b/src/main/resources/templates/bookInsertConfirm.html index 9a53f47..882221c 100644 --- a/src/main/resources/templates/bookInsertConfirm.html +++ b/src/main/resources/templates/bookInsertConfirm.html @@ -17,7 +17,7 @@ - +