This commit is contained in:
2026-09-28 13:40:48 +09:00
5 changed files with 94 additions and 54 deletions

View File

@@ -5,11 +5,14 @@ import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; 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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import com.example.demo.entity.BookInsert; import com.example.demo.entity.BookInsert;
import com.example.demo.repository.BookCategoryRepository; import com.example.demo.repository.BookCategoryRepository;
import com.example.demo.repository.StorageLocationRepository;
import com.example.demo.repository.SubCategoryRepository; import com.example.demo.repository.SubCategoryRepository;
import com.example.demo.service.BookInsertService; import com.example.demo.service.BookInsertService;
@@ -21,13 +24,18 @@ private BookInsertService bookInsertService;
@Autowired @Autowired
private BookCategoryRepository categoryRepository; private BookCategoryRepository categoryRepository;
@Autowired @Autowired
private SubCategoryRepository subCategoryRepository; private SubCategoryRepository subCategoryRepository;
@Autowired
private StorageLocationRepository storageLocationRepository;
@GetMapping("/bookInsert") @GetMapping("/bookInsert")
public String bookInsert(Model model) { public String bookInsert(Model model) {
model.addAttribute("categoryList",categoryRepository.findAll()); model.addAttribute("categoryList",categoryRepository.findAll());
model.addAttribute("subCategoryList",subCategoryRepository.findAll()); model.addAttribute("subCategoryList",subCategoryRepository.findAll());
model.addAttribute("locationList",storageLocationRepository.findAll());
model.addAttribute("bookInsert",new BookInsert()); model.addAttribute("bookInsert",new BookInsert());
return "bookInsert"; return "bookInsert";
} }
@@ -36,17 +44,37 @@ private SubCategoryRepository subCategoryRepository;
// 確認画面から戻る: POSTされた入力中の値がformに入ったまま入力画面へ // 確認画面から戻る: POSTされた入力中の値がformに入ったまま入力画面へ
@PostMapping("/bookInsertConfirm") @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); bookInsertService.save(bookInsert);
model.addAttribute("bookInsert",bookInsert); model.addAttribute("bookInsert",bookInsert);
return "bookInsertConfirm"; return "bookInsertConfirm";
} }
//確認画面から「もどる」ボタンを押したら入力画面に戻る
@PostMapping("/bookInsert") @PostMapping("/bookInsert")
public String bookInsertBack(BookInsert bookInsert,Model model) { public String bookInsertBack(BookInsert bookInsert,Model model) {
model.addAttribute("categoryList",categoryRepository.findAll()); model.addAttribute("categoryList",categoryRepository.findAll());
model.addAttribute("subCategoryList",new ArrayList<>()); if(bookInsert.getBookCategoryId() != null) {
model.addAttribute("bookInsert",bookInsert); 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"; return "bookInsert";
} }
@@ -54,18 +82,22 @@ private SubCategoryRepository subCategoryRepository;
public String changeCategory(BookInsert bookInsert,Model model) { public String changeCategory(BookInsert bookInsert,Model model) {
//カテゴリプルダウンに表示するデータを取得 //カテゴリプルダウンに表示するデータを取得
model.addAttribute("categoryList",categoryRepository.findAll()); 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); model.addAttribute("bookInsert", bookInsert);
return "bookInsert"; return "bookInsert";
} }
} }
//画面で選択したカテゴリbookInsert.getBookCategoryId())
//画面で選択したカテゴリbookInsert.getBookCategoryId()に選択した結果をいれる
//subCategoryRepository.findBookCategory_BookCategoryId(bookInsert.getBookCategoryId(1)));
//「カテゴリIDが1のサブカテゴリを取ってくる」
//model.addAttribute("subCategoryList",取得結果)

View File

@@ -8,6 +8,7 @@ import jakarta.persistence.GenerationType;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.Table; import jakarta.persistence.Table;
import jakarta.persistence.Transient; import jakarta.persistence.Transient;
import jakarta.validation.constraints.Pattern;
import lombok.Data; import lombok.Data;
@@ -27,6 +28,8 @@ public class BookInsert {
@Column(name = "title") @Column(name = "title")
private String title; private String title;
@Pattern(regexp = "^(?=(?:.*-){4})[0-9-]{17}$",
message = "ISBNは半角数字(13文字)とハイフン(4つ)を含めて入力してください")
@Column(name = "isbn") @Column(name = "isbn")
private String isbn; private String isbn;
@@ -40,7 +43,7 @@ public class BookInsert {
private String publisher; private String publisher;
@Column(name = "publication_date") @Column(name = "publication_date")
private String publicationDate; private LocalDate publicationDate;
@Column(name = "location_id") @Column(name = "location_id")
private Integer location; private Integer location;
@@ -66,7 +69,7 @@ public class BookInsert {
@Column(name = "book_approval") @Column(name = "book_approval")
private Integer bookApproval; private Integer bookApproval;
@Transient @Transient //DB保存無効
private Integer bookCategoryId; private Integer bookCategoryId;

View File

@@ -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<StorageLocation, Integer> {
}

View File

@@ -8,17 +8,12 @@
<body> <body>
<h1>新規図書登録入力</h1> <h1>新規図書登録入力</h1>
<br>
<br><br> <br><br>
<h4>新しく図書を登録します。必要事項を入力し、「登録」ボタンを押してください</h4>
<span style="color:red;">※は必須項目です</span>
<br><br>
<form action="/bookInsertConfirm" method="POST" th:object="${bookInsert}"> <form action="/bookInsertConfirm" method="POST" th:object="${bookInsert}">
<label for="bookId">図書ID</label><br>
<input type="text"
id="bookId"
placeholder="登録時に自動採番されます" readonly>
<br><br>
<label for="bookmanageId">図書管理ID</label><br> <label for="bookmanageId">図書管理ID</label><br>
<input type="text" <input type="text"
@@ -26,65 +21,58 @@
placeholder="登録時に自動採番されます" readonly> placeholder="登録時に自動採番されます" readonly>
<br><br> <br><br>
<label for="title">タイトル</label><br> <label for="title">タイトル<span style="color:red;">※</span></label><br>
<input type="text" id="title" name="title" placeholder="タイトルを入力" th:value="${bookInsert.title}" required> <input type="text" id="title" name="title" placeholder="タイトルを入力" th:value="${bookInsert.title}" required>
<br><br> <br><br>
<label for="isbn">ISBN</label><br> <label for="isbn">ISBN<span style="color:red;">※</span></label><br>
<input type="text" id="isbn" name="isbn" placeholder="ISBNを入力" th:value="${bookInsert.isbn}" required> <input type="text" id="isbn" name="isbn" placeholder="ISBNを入力" th:value="${bookInsert.isbn}" required>
<div th:if="${#fields.hasErrors('isbn')}" th:errors="*{isbn}" style="color:red">
</div>
<br><br> <br><br>
<label for="title">著者名</label><br> <label for="title">著者名<span style="color:red;">※</span></label><br>
<input type="text" id="author" name="author" placeholder="著者名を入力" th:value="${bookInsert.author}" required> <input type="text" id="author" name="author" placeholder="著者名を入力" th:value="${bookInsert.author}" required>
<br><br> <br><br>
<!--自動でサブかてを絞り込む。カテゴリをせんたくして、
このselectが入っているformの送信先を/bookInsert/categoryに変更、そのformを送信-->
<label for="category">カテゴリ</label><br> <label for="category">カテゴリ</label><br>
<select th:field="*{bookCategoryId}" <select th:field="*{bookCategoryId}" onchange="this.form.action='/bookInsert/category';this.form.submit();">
onchange=" <option value=""> 選択してください
this.form.action='/bookInsert/category'; </option>
this.form.submit();"> <option th:each="category:${categoryList}"
th:value="${category.bookCategoryId}"
<option value=""> th:text="${category.bookCategoryName}">
選択してください
</option>
<option th:each="category : ${categoryList}"
th:value="${category.bookCategoryId}"
th:text="${category.bookCategoryName}">
</option> </option>
</select> </select>
<br><br> <br><br>
<label for="subcategory">サブカテゴリ</label><br> <label for="subcategory">サブカテゴリ<span style="color:red;">※</span></label><br>
<select th:field="*{subcategory}"> <select th:field="*{subcategory}">
<option value="">選択してください</option> <option value="">選択してください</option>
<option th:each="sub:${subCategoryList}" <option th:each="sub:${subCategoryList}"
th:value="${sub.bookSubcategoryId}" th:value="${sub.bookSubcategoryId}"
th:text="${sub.bookSubcategoryName}"> th:text="${sub.bookSubcategoryName}" required>
</option> </option>
</select> </select>
<br><br> <br><br>
<label for="publisher">主パン者</label><br> <label for="location">保管場所<span style="color:red;">※</span></label><br>
<input type="text" id="publisher" name="publisher" th:value="${bookInsert.publisher}"> <select th:field="*{location}">
<option value="">選択してください</option>
<option th:each="location:${locationList}"
th:value="${location.locationId}"
th:text="${location.locationName}" required>
</option>
</select>
<br><br> <br><br>
<label for="publicationDate">出版日</label><br> <label for="condition">図書状態<span style="color:red;">※</span></label><br>
<input type="date" id="publicationDate" name="publicationDate" th:value="${bookInsert.publicationDate}"> <select th:field="*{condition}" required>
<br><br> <option value="">選択してください</option>
<label for="location">保管場所</label><br>
<select name="location">
<option value="1">未確定</option>
<option value="2">開発エリア書棚</option>
<option value="3">会議室前ラック</option>
</select>
<br><br>
<label for="condition">図書状態</label><br>
<select name="condition">
<option value="0">不明</option>
<option value="1">新品同様</option> <option value="1">新品同様</option>
<option value="2">良好</option> <option value="2">良好</option>
<option value="3">使用感あり</option> <option value="3">使用感あり</option>
@@ -92,6 +80,14 @@
</select> </select>
<br><br> <br><br>
<label for="publisher">出版社</label><br>
<input type="text" id="publisher" name="publisher" th:value="${bookInsert.publisher}">
<br><br>
<label for="publicationDate">出版日</label><br>
<input type="date" id="publicationDate" name="publicationDate" th:value="${bookInsert.publicationDate}">
<br><br>
<label for="donor">寄贈者社員ID</label><br> <label for="donor">寄贈者社員ID</label><br>
<input type="text" id="donor" name="donor" th:value="${bookInsert.donor}"> <input type="text" id="donor" name="donor" th:value="${bookInsert.donor}">
<br><br> <br><br>
@@ -103,7 +99,7 @@
</label>--> </label>-->
<input type="submit" value="確認画面へ" class="btn"> <input type="submit" value="登録" class="btn">
</form> </form>

View File

@@ -17,7 +17,7 @@
<input type="hidden" name="title" th:value="${bookInsert.title}"> <input type="hidden" name="title" th:value="${bookInsert.title}">
<input type="hidden" name="isbn" th:value="${bookInsert.isbn}"> <input type="hidden" name="isbn" th:value="${bookInsert.isbn}">
<input type="hidden" name="author" th:value="${bookInsert.author}"> <input type="hidden" name="author" th:value="${bookInsert.author}">
<input type="hidden" name="category" th:value="${bookInsert.bookCategoryId}"> <input type="hidden" name="bookCategoryId" th:value="${bookInsert.bookCategoryId}">
<input type="hidden" name="subcategory" th:value="${bookInsert.subcategory}"> <input type="hidden" name="subcategory" th:value="${bookInsert.subcategory}">
<input type="hidden" name="publisher" th:value="${bookInsert.publisher}"> <input type="hidden" name="publisher" th:value="${bookInsert.publisher}">
<input type="hidden" name="publicationDate" th:value="${bookInsert.publicationDate}"> <input type="hidden" name="publicationDate" th:value="${bookInsert.publicationDate}">