入力チェック修正

This commit is contained in:
m-endo
2026-10-26 13:00:57 +09:00
parent b675695692
commit 7b9c62b98b
6 changed files with 51 additions and 89 deletions

View File

@@ -1,7 +1,5 @@
package com.example.demo.controller; package com.example.demo.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -40,7 +38,7 @@ public class LendingController {
public String showLendingForm(Model model) { public String showLendingForm(Model model) {
LendingForm form = new LendingForm(); LendingForm form = new LendingForm();
//以下ダミーデータを挿入中 //以下ダミーデータを挿入中、画面遷移変更後削除する
form.setIdBook(1); form.setIdBook(1);
form.setNmBook("これからはじめるJava言語"); form.setNmBook("これからはじめるJava言語");
model.addAttribute("lendingForm", form); model.addAttribute("lendingForm", form);
@@ -73,17 +71,15 @@ public class LendingController {
//@PostMappingでデータを送信(POST) //@PostMappingでデータを送信(POST)
@PostMapping("/confirmLending") @PostMapping("/confirmLending")
public String confirmLending(@ModelAttribute("lendingForm") LendingForm form, public String confirmLending(@ModelAttribute("lendingForm") LendingForm form,
//インタフェースBindingResultにて入力結果を表す BindingResult result, //
BindingResult result,
Model model) { Model model) {
//日付チェック validateParameter(form, result);
List<String> errorList = validateParameter(form);
// エラーが1つでもある場合入力画面に戻す // result.hasErrors() でエラー判定
if (!errorList.isEmpty()) { if (result.hasErrors()) {
model.addAttribute("errors", errorList); // エラーが1つでもあったら入力画面に戻す
return "application"; // 入力画面に戻す return "application";
} }
return "applicationConfirm"; return "applicationConfirm";
@@ -128,36 +124,24 @@ public class LendingController {
return "applicationComplete"; return "applicationComplete";
} }
//画面でエラーが出た場合の表示 // 画面でエラーが出た場合の表示
//エラーが出た場合、リストに文言を追加する // エラーが出た場合、BindingResultに直接エラーを登録する
public List<String> validateParameter(LendingForm form) { public void validateParameter(LendingForm form, BindingResult result) {
final String DATE_BORROW_NULL = "貸出日 が未入力です。";
final String DATE_RETURN_NULL = "返却予定日 が未入力です。";
final String DATE_ORDER_ERROR = "返却予定日 には、貸出日以降の日付を選択してください。";
List<String> error = new ArrayList<>();
// 1.「貸出日」が未入力(null)かどうかをチェックする // 1.「貸出日」が未入力(null)かどうかをチェックする
if (form.getDateBorrow() == null) { if (form.getDateBorrow() == null) {
//errorリストの中にメッセージ"貸出日が未入力です"を入れる result.rejectValue("dateBorrow", "error.dateBorrow", "貸出日 が未入力です。");
error.add(DATE_BORROW_NULL);
} }
// 2.「返却予定日」が未入力(null)かどうかをチェック // 2.「返却予定日」が未入力(null)かどうかをチェック
if (form.getDateScheduledReturn() == null) { if (form.getDateScheduledReturn() == null) {
//errorリストの中にメッセージ"返却予定日 が未入力です。"を入れる result.rejectValue("dateScheduledReturn", "error.dateScheduledReturn", "返却予定日 が未入力です。");
error.add(DATE_RETURN_NULL);
} }
// 3「両方ともnullではない(入力されている)」ときのみ日付の前後を比較する // 3「両方ともnullではない(入力されている)」ときのみ日付の前後を比較する
if (form.getDateBorrow() != null && form.getDateScheduledReturn() != null) { if (form.getDateBorrow() != null && form.getDateScheduledReturn() != null) {
// 返却予定日が貸出日より過去の場合のエラー
if (form.getDateScheduledReturn().before(form.getDateBorrow())) { if (form.getDateScheduledReturn().before(form.getDateBorrow())) {
//errorリストの中にメッセージ"返却予定日 には、貸出日以降の日付を選択してください。"を入れる // 日付の前後エラーを返却予定日のエラーとして登録
error.add(DATE_ORDER_ERROR); result.rejectValue("dateScheduledReturn", "error.dateOrder", "返却予定日 には、貸出日以降の日付を選択してください。");
} }
} }
return error;
} }
} }

View File

@@ -1,46 +1,20 @@
package com.example.demo.controller; package com.example.demo.controller;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
@Controller @Controller
public class LogoutController { public class LogoutController {
// // ログ出力用インスタンス @Autowired
// private final static Logger LOGGER = Logger.getLogger(LogoutController.class.getName()); HttpSession session;
// // エラーメッセージ用の定数フィールド
// private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!"; @PostMapping("/doLogout")
// public String doLogout() {
// HttpSession httpSession; //セッションをすべて破棄
// session.invalidate();
// @Autowired return "login";
// public LogoutController(HttpSession httpSession) { }
// this.httpSession = httpSession;
// }
//
// @ModelAttribute
// public LoginUserForm loginUserForm() {
// return new LoginUserForm();
// }
//
// @PermissionCheck
// @SessionCheck
// @RequestMapping(value = LOGOUT, method = RequestMethod.POST)
// public String doLogout(Model model) {
// try {
// // セッション情報の破棄
// httpSession.invalidate();
//
// // ログイン画面に遷移するための情報を設定
// model.addAttribute(PageReturnAttributeKeyword.LOGIN_INFO_FORM, loginUserForm());
// // login.html に遷移
// return TransitionTargetPageNameKeyword.LOGIN_HTML;
//
// } catch (Exception e) {
//
// // エラーログ出力
// LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
// // エラーページに遷移
// return TransitionTargetPageNameKeyword.ERROR_HTML;
// }
//
// }
} }

View File

@@ -27,7 +27,7 @@ public class LendingForm {
* 貸出日 * 貸出日
*/ */
@DateTimeFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd")
//LocalDate型にて日付(年・月・日)を取り扱う //Date型で取り扱う
private Date dateBorrow; private Date dateBorrow;
/** /**

View File

@@ -9,17 +9,19 @@
</head> </head>
<body> <body>
<!--タイトル表示-->
<h1>書籍貸出申請</h1>
<!--右上にログイン者名、ログアウトボタン配置--> <!--右上にログイン者名、ログアウトボタン配置-->
<!--現在ダミー配置--> <!--現在ダミー配置-->
<div class="rightSection"> <div class="rightSection">
<span class="loginInfo">ログイン者名:<span>テスト太郎(ダミー)</span></span>
<button class="logoutButton" onclick="logoutSubmit()">ログアウト</button> <button class="logoutButton" onclick="logoutSubmit()">ログアウト</button>
<span class="loginInfo">ログイン者名:<span>テスト太郎(ダミー)</span></span>
</div> </div>
<!--配置予定の記述--> <!--配置予定の記述-->
<!-- <div class="rightSection"> <!-- <div class="rightSection">
<span class="loginInfo">ログイン者名:<span th:text="${session.loginUser.nmEmployee}"></span></span> <button class="logoutButton" onclick="logoutSubmit()">ログアウト</button>
<button class="logoutButton" onclick="logoutSubmit()">ログアウト</button> <span class="loginInfo">ログイン者名:<span th:text="${session.loginUser.nmEmployee}"></span>
</div> </div>
--> -->
<!--クラス名確認すること(現在仮入力)--> <!--クラス名確認すること(現在仮入力)-->

View File

@@ -9,6 +9,8 @@
</head> </head>
<body> <body>
<!--タイトル表示-->
<h1>書籍貸出申請</h1>
<!--右上にログイン者名、ログアウトボタン配置--> <!--右上にログイン者名、ログアウトボタン配置-->
<!--現在ダミー配置--> <!--現在ダミー配置-->
<div class="rightSection"> <div class="rightSection">
@@ -17,19 +19,18 @@
</div> </div>
<!--配置予定の記述--> <!--配置予定の記述-->
<!-- <div class="rightSection"> <!-- <div class="rightSection">
<span class="loginInfo">ログイン者名:<span th:text="${session.loginUser.nmEmployee}"></span></span> <button class="logoutButton" onclick="logoutSubmit()">ログアウト</button>
<button class="logoutButton" onclick="logoutSubmit()">ログアウト</button> <span class="loginInfo">ログイン者名:<span th:text="${session.loginUser.nmEmployee}"></span>
</div> </div>
--> -->
<!--見出し表示--> <!--見出し表示-->
<h1>書籍貸出申請完了しました</h1> <h2>書籍貸出申請完了しました</h2>
<div class="navigation-links"> <div class="navigation-links">
<!--書籍一覧に戻る処理、URL確認を行う--> <!--書籍一覧に戻る処理、URL確認を行う-->
<a th:href="@{/bookList}"> 書籍一覧に戻る</a> <a th:href="@{/bookList}"> 書籍一覧に戻る</a>
| 
<!--メニュー画面に戻る処理、URL確認を行う--> <!--メニュー画面に戻る処理、URL確認を行う-->
<a th:href="@{/menu}">メニューに戻る</a> <a th:href="@{/menu}">メニューに戻る</a>
</div> </div>

View File

@@ -8,6 +8,8 @@
</head> </head>
<body> <body>
<!--タイトル表示-->
<h1>書籍貸出申請</h1>
<!--右上にログイン者名、ログアウトボタン配置--> <!--右上にログイン者名、ログアウトボタン配置-->
<!--現在ダミー配置--> <!--現在ダミー配置-->
<div class="rightSection"> <div class="rightSection">
@@ -16,14 +18,13 @@
</div> </div>
<!--配置予定の記述--> <!--配置予定の記述-->
<!-- <div class="rightSection"> <!-- <div class="rightSection">
<span class="loginInfo">ログイン者名:<span th:text="${session.loginUser.nmEmployee}"></span></span> <button class="logoutButton" onclick="logoutSubmit()">ログアウト</button>
<button class="logoutButton" onclick="logoutSubmit()">ログアウト</button> <span class="loginInfo">ログイン者名:<span th:text="${session.loginUser.nmEmployee}"></span>
</div> </div>
--> -->
<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="*{idBook}" /> <input type="hidden" th:field="*{idBook}" />
<input type="hidden" th:field="*{nmBook}" /> <input type="hidden" th:field="*{nmBook}" />
<input type="hidden" th:field="*{dateBorrow}" /> <input type="hidden" th:field="*{dateBorrow}" />