controllerクラス作成、画面遷移確認

合わせてserviceクラスへ不足分の記述追加、HTML、formクラスの記述を変更
This commit is contained in:
m-endo
2026-10-23 11:10:09 +09:00
parent 35ec8d994a
commit 1ebadc2540
9 changed files with 109 additions and 102 deletions

View File

@@ -1,9 +1,9 @@
package com.example.demo.controller; package com.example.demo.controller;
import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
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.BindingResult;
@@ -12,18 +12,19 @@ import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import com.example.demo.form.LendingForm; import com.example.demo.form.LendingForm;
import com.example.demo.service.ReturnService;
@Controller @Controller
public class LendingController { public class LendingController {
//(仮記入)まとめられたserviceクラスを入れる準備 //まとめられたserviceクラスを入れる準備
//private final LendingService lendingService; private final ReturnService returnService;
//(仮記入)アノテーションを使用してコンストラクタにインスタンスを代入する //アノテーションを使用してコンストラクタにインスタンスを代入する
//@Autowired @Autowired
//public LendingController(LendingService lendingService) { public LendingController(ReturnService returnService) {
//this.LendingService lendingService; this.returnService = returnService;
//} }
/** /**
* 【画面1】書籍貸出申請入力画面を表示する * 【画面1】書籍貸出申請入力画面を表示する
@@ -33,8 +34,8 @@ public class LendingController {
LendingForm form = new LendingForm(); LendingForm form = new LendingForm();
//以下ダミーデータを挿入中 //以下ダミーデータを挿入中
form.setId_book(1); form.setIdBook(1);
form.setNm_book("これからはじめるJava言語"); form.setNmBook("これからはじめるJava言語");
model.addAttribute("lendingForm", form); model.addAttribute("lendingForm", form);
return "application"; return "application";
@@ -51,8 +52,8 @@ public class LendingController {
// LendingForm form = new LendingForm(); // LendingForm form = new LendingForm();
// //
//前の画面(bookList)から貰ったデータの移し替え //前の画面(bookList)から貰ったデータの移し替え
// form.setIdBook(idBook); // form.setId_book(idBook);
// form.setNmBook(nmBook); // form.setNm_book(nmBook);
// //
//次の画面(application.html)へ渡す //次の画面(application.html)へ渡す
// model.addAttribute("lendingForm", form); // model.addAttribute("lendingForm", form);
@@ -97,18 +98,20 @@ public class LendingController {
/** /**
* 確認画面の「申請」ボタンを押して完了画面へ遷移 * 確認画面の「申請」ボタンを押して完了画面へ遷移
*/ */
//@PostMappingでデータを送信(POST) // @PostMappingでデータを送信(POST)
@PostMapping("/executeLending") @PostMapping("/executeLending")
public String executeLending(@ModelAttribute("lendingForm") LendingForm form) { public String executeLending(@ModelAttribute("lendingForm") LendingForm form) {
//redirect記載で完了画面のURLへ接続し直す
// 1. 仮のログイン社員ID準備
int loginEmployeeId = 1;
// 2. サービスクラス内の貸出申請機能を呼び出しを行う
returnService.executeLending(form, loginEmployeeId);
// 3. redirectメソッドにて次画面である完了画面のURLへ接続し直す
return "redirect:/lendingComplete"; return "redirect:/lendingComplete";
} }
//(仮記入)まとめられたserviceクラスの呼び出し
//lendingService.executeLending(form);
//redirectメソッドにて次画面である完了画面のURLへ接続し直す
//return "redirect:/lendingComplete";
/** /**
* 書籍貸出申請完了画面を表示する * 書籍貸出申請完了画面を表示する
*/ */
@@ -118,14 +121,6 @@ public class LendingController {
return "applicationComplete"; return "applicationComplete";
} }
//isNullメソッドを作り、未入力になってないか入力チェックを行う
/**
* 日付用の null 判定メソッド
*/
private static boolean isNull(LocalDate date) {
return null == date;
}
//画面でエラーが出た場合の表示 //画面でエラーが出た場合の表示
//エラーが出た場合、リストに文言を追加する //エラーが出た場合、リストに文言を追加する
public List<String> validateParameter(LendingForm form) { public List<String> validateParameter(LendingForm form) {
@@ -136,21 +131,21 @@ public class LendingController {
List<String> error = new ArrayList<>(); List<String> error = new ArrayList<>();
// 1.「貸出日」が未入力(null)かどうかをチェックする // 1.「貸出日」が未入力(null)かどうかをチェックする
if (form.getDate_borrow() == null) { if (form.getDateBorrow() == null) {
//errorリストの中にメッセージ"貸出日が未入力です"を入れる //errorリストの中にメッセージ"貸出日が未入力です"を入れる
error.add(DATE_BORROW_NULL); error.add(DATE_BORROW_NULL);
} }
// 2.「返却予定日」が未入力(null)かどうかをチェック // 2.「返却予定日」が未入力(null)かどうかをチェック
if (form.getDate_scheduled_return() == null) { if (form.getDateScheduledReturn() == null) {
//errorリストの中にメッセージ"返却予定日 が未入力です。"を入れる //errorリストの中にメッセージ"返却予定日 が未入力です。"を入れる
error.add(DATE_RETURN_NULL); error.add(DATE_RETURN_NULL);
} }
// 3「両方ともnullではない(入力されている)」ときのみ日付の前後を比較する // 3「両方ともnullではない(入力されている)」ときのみ日付の前後を比較する
if (form.getDate_borrow() != null && form.getDate_scheduled_return() != null) { if (form.getDateBorrow() != null && form.getDateScheduledReturn() != null) {
// 返却予定日が貸出日より過去の場合のエラー // 返却予定日が貸出日より過去の場合のエラー
if (form.getDate_scheduled_return().isBefore(form.getDate_borrow())) { if (form.getDateScheduledReturn().before(form.getDateBorrow())) {
//errorリストの中にメッセージ"返却予定日 には、貸出日以降の日付を選択してください。"を入れる //errorリストの中にメッセージ"返却予定日 には、貸出日以降の日付を選択してください。"を入れる
error.add(DATE_ORDER_ERROR); error.add(DATE_ORDER_ERROR);
} }

View File

@@ -72,13 +72,13 @@ public class Borrow_booksEntity {
* 貸出日 * 貸出日
* */ * */
@Column(name = "date_borrow", nullable = false) @Column(name = "date_borrow", nullable = false)
private Date dateBorrow; private Date dateBorrow = new Date();
/* /*
* 返却予定日 * 返却予定日
* */ * */
@Column(name = "date_scheduled_return", nullable = false) @Column(name = "date_scheduled_return", nullable = false)
private Date dateScheduledReturn; private Date dateScheduledReturn = new Date();
/* /*
* 返却日 * 返却日

View File

@@ -1,6 +1,6 @@
package com.example.demo.form; package com.example.demo.form;
import java.time.LocalDate; import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
@@ -15,11 +15,11 @@ public class LendingForm {
/** /**
* 蔵書ID * 蔵書ID
*/ */
private int id_book; private int idBook;
/** /**
* 書籍名 * 書籍名
*/ */
private String nm_book; private String nmBook;
//貸出日と返却予定日にアノテーションで日付や時刻の表示形式を指定 //貸出日と返却予定日にアノテーションで日付や時刻の表示形式を指定
@@ -28,26 +28,33 @@ public class LendingForm {
*/ */
@DateTimeFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd")
//LocalDate型にて日付(年・月・日)を取り扱う //LocalDate型にて日付(年・月・日)を取り扱う
private LocalDate date_borrow; private Date dateBorrow;
/** /**
* 返却予定日 * 返却予定日
*/ */
@DateTimeFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate date_scheduled_return; private Date dateScheduledReturn;
/** /**
* 貸出日を取得するメソッド * 貸出日を取得するメソッド
*/ */
public LocalDate getDate_borrow() { public Date getDate_borrow() {
return this.date_borrow; return this.dateBorrow;
} }
/** /**
* 返却予定日を取得するメソッド * 返却予定日を取得するメソッド
*/ */
public LocalDate getDate_scheduled_return() { public Date getDate_scheduled_return() {
return this.date_scheduled_return; return this.dateScheduledReturn;
}
/**
* サービスクラスの表記に合わせるためのメソッド
*/
public int getId_book() {
return this.idBook;
} }
} }

View File

@@ -1,6 +1,7 @@
package com.example.demo.repository; package com.example.demo.repository;
import java.util.List; import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
@@ -25,4 +26,7 @@ public interface Book_CollectionRepository extends JpaRepository<Book_collection
@Param("nmBook") String nmBook, @Param("nmBook") String nmBook,
@Param("pubLisher") String pubLisher, @Param("pubLisher") String pubLisher,
@Param(" noteText") String noteText); @Param(" noteText") String noteText);
//findByIDメソッドを明記しデータベースの主キーを指定、entity取得
Optional<Book_collectionEntity>findById(Integer id);
} }

View File

@@ -146,13 +146,16 @@ public class ReturnService {
book_CollectionRepository.save(book_collection.get()); book_CollectionRepository.save(book_collection.get());
Borrow_booksEntity borrow = new Borrow_booksEntity();//空の申請レコード Borrow_booksEntity borrow = new Borrow_booksEntity();//空の申請レコード
borrow.setIdApproval(idEmployee);//申請者ID borrow.setIdApplicant(idEmployee);//申請者ID
borrow.setIdBook(form.getId_book());//蔵書ID borrow.setIdBook(form.getId_book());//蔵書ID
borrow.setDateRequest(new Date());//申請日 borrow.setDateRequest(new Date());//申請日
borrow.setStatus('1');//ステータスを承認待ちに borrow.setStatus('1');//ステータスを承認待ちに
borrow.setIdUpdate(idEmployee);//更新者ID borrow.setIdUpdate(idEmployee);//更新者ID
borrow.setDateUpdate(new Date());//更新日 borrow.setDateUpdate(new Date());//更新日
//必要だったため追記
borrow.setDateBorrow(form.getDate_borrow()); //貸出日
borrow.setDateScheduledReturn(form.getDate_scheduled_return()); //返却予定日
borrowRepository.save(borrow); borrowRepository.save(borrow);
} }
} }

View File

@@ -1,44 +1,34 @@
package com.example.demo.service.Impl; package com.example.demo.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.demo.form.LendingForm;
import com.example.demo.repository.Book_CollectionRepository;
import com.example.demo.repository.Borrow_BooksRepository;
import com.example.demo.repository.EmployeeRepository;
import com.example.demo.service.LendingService;
/** /**
* 書籍貸出申請の処理をまとめたサービスクラス * 書籍貸出申請の処理をまとめたサービスクラス
*/ */
@Service //@Service
@Transactional //@Transactional
public class LendingServiceImpl implements LendingService { //public class LendingServiceImpl implements LendingService {
//3つリポジトリクラス記載 // //3つリポジトリクラス記載
private final Book_CollectionRepository bookCollectionRepository; // private final Book_CollectionRepository bookCollectionRepository;
private final Borrow_BooksRepository borrowBooksRepository; // private final Borrow_BooksRepository borrowBooksRepository;
private final EmployeeRepository employeeRepository; // private final EmployeeRepository employeeRepository;
//
// //@Autwiredでリポジトリを自動で接続
// @Autowired
// public LendingServiceImpl(Book_CollectionRepository bookCollectionRepository,
// Borrow_BooksRepository borrowBooksRepository, EmployeeRepository employeeRepository) {
// this.bookCollectionRepository = bookCollectionRepository;
// this.borrowBooksRepository = borrowBooksRepository;
// this.employeeRepository = employeeRepository;
// }
//
// /**
// * 【本番処理】貸出申請データを組み立ててデータベースへ保存
// */
//
// @Override
// public void executeLending(LendingForm form) {
//
// //画面の蔵書IDをEntityに合わせるために変換
// //Integer bookId = Integer.parseUnsignedInt(form.getId_book());
// }
//@Autwiredでリポジトリを自動で接続
@Autowired
public LendingServiceImpl(Book_CollectionRepository bookCollectionRepository,
Borrow_BooksRepository borrowBooksRepository, EmployeeRepository employeeRepository) {
this.bookCollectionRepository = bookCollectionRepository;
this.borrowBooksRepository = borrowBooksRepository;
this.employeeRepository = employeeRepository;
}
/**
* 【本番処理】貸出申請データを組み立ててデータベースへ保存
*/
@Override
public void executeLending(LendingForm form) {
//画面の蔵書IDをEntityに合わせるために変換
//Integer bookId = Integer.parseUnsignedInt(form.getId_book());
}
}

View File

@@ -15,7 +15,7 @@
<span class="loginInfo">ログイン者名:<span>テスト太郎(ダミー)</span></span> <span class="loginInfo">ログイン者名:<span>テスト太郎(ダミー)</span></span>
<button class="logoutButton" onclick="logoutSubmit()">ログアウト</button> <button class="logoutButton" onclick="logoutSubmit()">ログアウト</button>
</div> </div>
<!--本来配置予定の記述--> <!--配置予定の記述-->
<!-- <div class="rightSection"> <!-- <div class="rightSection">
<span class="loginInfo">ログイン者名:<span th:text="${session.loginUser.nmEmployee}"></span></span> <span class="loginInfo">ログイン者名:<span th:text="${session.loginUser.nmEmployee}"></span></span>
<button class="logoutButton" onclick="logoutSubmit()">ログアウト</button> <button class="logoutButton" onclick="logoutSubmit()">ログアウト</button>
@@ -28,25 +28,30 @@
<!--蔵書ID、書籍名はreadonlyにて固定--> <!--蔵書ID、書籍名はreadonlyにて固定-->
<tr> <tr>
<th class="header">蔵書ID</th> <th class="header">蔵書ID</th>
<td><input type="text" th:field="*{id_book}" readonly="readonly"></td> <td><input type="text" th:field="*{idBook}" readonly="readonly"></td>
</tr> </tr>
<tr> <tr>
<th class="header">書籍名</th> <th class="header">書籍名</th>
<td><input type="text" th:field="*{nm_book}" readonly="readonly"></td> <td><input type="text" th:field="*{nmBook}" readonly="readonly"></td>
</tr> </tr>
<tr> <tr>
<!--input type ="date"にてカレンダー入力表示--> <!--input type ="date"にてカレンダー入力表示-->
<!--required属性を追加、未入力をブラウザ側ではじく--> <!--required属性を追加、未入力をブラウザ側ではじく-->
<!--Thymeleafにてエラー表示エリアを作成--> <!--Thymeleafにてエラー表示エリアを作成-->
<th class="header">貸出日</th> <th class="header">貸出日</th>
<td><input type="date" th:field="*{date_borrow}" required></td> <td>
<span th:if="${#fields.hasErrors('date_borrow')}" th:errors="*{date_borrow}" style="color:red;"></span> <input type="date" th:field="*{dateBorrow}" required>
<span th:if="${#fields.hasErrors('dateBorrow')}" th:errors="*{dateBorrow}"
style="color:red;"></span>
</td>
</tr> </tr>
<tr> <tr>
<th class="header">返却予定日</th> <th class="header">返却予定日</th>
<td><input type="date" th:field="*{date_scheduled_return}" required></td> <td>
<span th:if="${#fields.hasErrors('date_scheduled_return')}" th:errors="*{date_scheduled_return}" <input type="date" th:field="*{dateScheduledReturn}" required>
style="color:red;"></span> <span th:if="${#fields.hasErrors('dateScheduledReturn')}" th:errors="*{dateScheduledReturn}"
style="color:red;"></span>
</td>
</tr> </tr>
<tr> <tr>
<td class="btnArea" colspan="2"> <td class="btnArea" colspan="2">

View File

@@ -15,7 +15,7 @@
<span class="loginInfo">ログイン者名:<span>テスト太郎(ダミー)</span></span> <span class="loginInfo">ログイン者名:<span>テスト太郎(ダミー)</span></span>
<button class="logoutButton" onclick="logoutSubmit()">ログアウト</button> <button class="logoutButton" onclick="logoutSubmit()">ログアウト</button>
</div> </div>
<!--本来配置予定の記述--> <!--配置予定の記述-->
<!-- <div class="rightSection"> <!-- <div class="rightSection">
<span class="loginInfo">ログイン者名:<span th:text="${session.loginUser.nmEmployee}"></span></span> <span class="loginInfo">ログイン者名:<span th:text="${session.loginUser.nmEmployee}"></span></span>
<button class="logoutButton" onclick="logoutSubmit()">ログアウト</button> <button class="logoutButton" onclick="logoutSubmit()">ログアウト</button>

View File

@@ -1,3 +1,5 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<!--書籍貸出申請確認画面--> <!--書籍貸出申請確認画面-->
<head> <head>
@@ -12,7 +14,7 @@
<span class="loginInfo">ログイン者名:<span>テスト太郎(ダミー)</span></span> <span class="loginInfo">ログイン者名:<span>テスト太郎(ダミー)</span></span>
<button class="logoutButton" onclick="logoutSubmit()">ログアウト</button> <button class="logoutButton" onclick="logoutSubmit()">ログアウト</button>
</div> </div>
<!--本来配置予定の記述--> <!--配置予定の記述-->
<!-- <div class="rightSection"> <!-- <div class="rightSection">
<span class="loginInfo">ログイン者名:<span th:text="${session.loginUser.nmEmployee}"></span></span> <span class="loginInfo">ログイン者名:<span th:text="${session.loginUser.nmEmployee}"></span></span>
<button class="logoutButton" onclick="logoutSubmit()">ログアウト</button> <button class="logoutButton" onclick="logoutSubmit()">ログアウト</button>
@@ -21,31 +23,32 @@
<form th:action="@{/executeLending}" id="lendingConfirmForm" method="post" th:object="${lendingForm}"> <form th:action="@{/executeLending}" id="lendingConfirmForm" method="post" th:object="${lendingForm}">
<!--Java側へデータを引き継ぐための隠しフィールド(必須) --> <!--Java側へデータを引き継ぐための隠しフィールド(必須) -->
<input type="hidden" th:field="*{id_book}" /> <!--変数の記載変更を行うこと-->
<input type="hidden" th:field="*{nm_book}" /> <input type="hidden" th:field="*{idBook}" />
<input type="hidden" th:field="*{date_borrow}" /> <input type="hidden" th:field="*{nmBook}" />
<input type="hidden" th:field="*{date_scheduled_return}" /> <input type="hidden" th:field="*{dateBorrow}" />
<input type="hidden" th:field="*{dateScheduledReturn}" />
<table> <table>
<tr> <tr>
<!-- 以下4項目<span>タグを用いてテキスト表示として画面に固定表示 --> <!-- 以下4項目<span>タグを用いてテキスト表示として画面に固定表示 -->
<th class="header">蔵書ID</th> <th class="header">蔵書ID</th>
<td><span th:text="*{id_book}"></span></td> <td><span th:text="*{idBook}"></span></td>
</tr> </tr>
<tr> <tr>
<th class="header">書籍名</th> <th class="header">書籍名</th>
<td><span th:text="*{nm_book}"></span></td> <td><span th:text="*{nmBook}"></span></td>
</tr> </tr>
<tr> <tr>
<th class="header">貸出日</th> <th class="header">貸出日</th>
<td><span <td><span
th:text="*{date_borrow} != null ? ${#temporals.format(lendingForm.date_borrow, 'yyyy/MM/dd')} : ''"></span> th:text="*{dateBorrow} != null ? ${#dates.format(lendingForm.dateBorrow, 'yyyy/MM/dd')} : ''"></span>
</td> </td>
</tr> </tr>
<tr> <tr>
<th class="header">返却予定日</th> <th class="header">返却予定日</th>
<td><span <td><span
th:text="*{date_scheduled_return} != null ? ${#temporals.format(lendingForm.date_scheduled_return, 'yyyy/MM/dd')} : ''"></span> th:text="*{dateScheduledReturn} != null ? ${#dates.format(lendingForm.dateScheduledReturn, 'yyyy/MM/dd')} : ''"></span>
</td> </td>
</tr> </tr>
<tr> <tr>