[edit]バグ修正をしました

This commit is contained in:
2026-09-29 15:36:22 +09:00
parent c5c5c3dcc9
commit 47ce48e323

View File

@@ -18,45 +18,47 @@ import com.example.demo.service.BookInsertService;
@Controller @Controller
public class BookInsertController { public class BookInsertController {
@Autowired @Autowired
private BookInsertService bookInsertService; private BookInsertService bookInsertService;
@Autowired @Autowired
private BookCategoryRepository categoryRepository; private BookCategoryRepository categoryRepository;
@Autowired @Autowired
private SubCategoryRepository subCategoryRepository; private SubCategoryRepository subCategoryRepository;
@Autowired @Autowired
private StorageLocationRepository storageLocationRepository; private StorageLocationRepository storageLocationRepository;
@Autowired @Autowired
private BookInsertRepository bookInsertRepository; private BookInsertRepository bookInsertRepository;
@GetMapping("/bookInsert") @GetMapping("/bookInsert")
public String bookInsert(Model model) { public String bookInsert(Model model) {
model.addAttribute("nextBookManageId",bookInsertService.createBookManageId()); model.addAttribute("nextBookManageId", bookInsertService.createBookManageId());
model.addAttribute("bookInsert",new BookInsert()); model.addAttribute("bookInsert", new BookInsert());
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("locationList", storageLocationRepository.findAll());
model.addAttribute("bookInsert",new BookInsert()); model.addAttribute("bookInsert", new BookInsert());
return "bookInsert"; return "bookInsert";
} }
// 初期表示: 空のformを渡す(th:fieldの初期値が空になる) // 初期表示: 空のformを渡す(th:fieldの初期値が空になる)
// 確認画面から戻る: POSTされた入力中の値がformに入ったまま入力画面へ // 確認画面から戻る: POSTされた入力中の値がformに入ったまま入力画面へ
//入力内容確認してOKであれば確認画面へ
@PostMapping("/bookInsertConfirm") @PostMapping("/bookInsertConfirm")
public String bookInsertConfirm(@Validated BookInsert bookInsert,BindingResult result,Model model) { public String bookInsertConfirm(@Validated BookInsert bookInsert, BindingResult result, Model model) {
if(result.hasErrors()) { if (result.hasErrors()) {
model.addAttribute("nextBookManageId",bookInsertService.createBookManageId()); model.addAttribute("nextBookManageId", bookInsertService.createBookManageId());
model.addAttribute("categoryList",categoryRepository.findAll()); model.addAttribute("categoryList", categoryRepository.findAll());
if(bookInsert.getBookCategoryId() != null) { //nullじゃなかったら if (bookInsert.getBookCategoryId() != null) { //nullじゃなかったら
model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); model.addAttribute("subCategoryList",
subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId()));
//そのカテゴリに属するサブカテゴリを持ってくる //そのカテゴリに属するサブカテゴリを持ってくる
} else{ } else {
model.addAttribute("subCategoryList",subCategoryRepository.findAll()); model.addAttribute("subCategoryList", subCategoryRepository.findAll());
} }
model.addAttribute("locationList",storageLocationRepository.findAll()); model.addAttribute("locationList",storageLocationRepository.findAll());
model.addAttribute("bookInsert", bookInsert); model.addAttribute("bookInsert", bookInsert);
@@ -64,32 +66,23 @@ private BookInsertRepository bookInsertRepository;
} }
bookInsertService.save(bookInsert); bookInsertService.save(bookInsert);
model.addAttribute("bookInsert",bookInsert); model.addAttribute("bookInsert", bookInsert);
return "bookInsertConfirm"; return "bookInsertConfirm";
} }
//確認画面から「もどる」ボタンを押したら入力画面に戻る
@PostMapping("/bookInsert")
public String bookInsertBack(BookInsert bookInsert,Model model) {
model.addAttribute("nextBookManageId",bookInsertService.createBookManageId());
model.addAttribute("categoryList",categoryRepository.findAll());
if(bookInsert.getBookCategoryId() !=null) {
model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId()));
} else{
model.addAttribute("subCategoryList",subCategoryRepository.findAll());
}
model.addAttribute("locationList",storageLocationRepository.findAll());
model.addAttribute("bookInsert",bookInsert);
return "bookInsert";
}
@PostMapping("/bookInsert/category") @PostMapping("/bookInsert/category")
public String changeCategory(BookInsert bookInsert,Model model) { public String changeCategory(BookInsert bookInsert, Model model) {
//カテゴリプルダウンに表示するデータを取得 //カテゴリプルダウンに表示するデータを取得
model.addAttribute("nextBookManageId",bookInsertService.createBookManageId()); model.addAttribute("nextBookManageId", bookInsertService.createBookManageId());
model.addAttribute("categoryList",categoryRepository.findAll()); model.addAttribute("categoryList", categoryRepository.findAll());
model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); if (bookInsert.getBookCategoryId() != null) { //nullじゃなかったら
model.addAttribute("locationList",storageLocationRepository.findAll()); model.addAttribute("subCategoryList",
subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId()));
//そのカテゴリに属するサブカテゴリを持ってくる
} else {
model.addAttribute("subCategoryList", subCategoryRepository.findAll());
}
model.addAttribute("locationList", storageLocationRepository.findAll());
model.addAttribute("bookInsert", bookInsert); model.addAttribute("bookInsert", bookInsert);
return "bookInsert"; return "bookInsert";
@@ -97,13 +90,7 @@ private BookInsertRepository bookInsertRepository;
} }
//画面で選択したカテゴリbookInsert.getBookCategoryId()に選択した結果をいれる
//subCategoryRepository.findBookCategory_BookCategoryId(bookInsert.getBookCategoryId(1)));
//画面で選択したカテゴリbookInsert.getBookCategoryId()に選択した結果をいれる //「カテゴリIDが1のサブカテゴリを取ってくる」
//subCategoryRepository.findBookCategory_BookCategoryId(bookInsert.getBookCategoryId(1))); //model.addAttribute("subCategoryList",取得結果)
//「カテゴリIDが1のサブカテゴリを取ってくる」
//model.addAttribute("subCategoryList",取得結果)