質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

1回答

2107閲覧

【Java_SpringBoot】入力チェックの結果に応じてエラーメッセージを画面に表示したい

odaken

総合スコア0

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2021/03/23 16:41

前提・実現したいこと

Spring BootでWebアプリ作成勉強中のものです。
入力チェックをグループに分けて実施し、エラーがあった場合は画面にエラーメッセージを
表示したいのですが、エラーメッセージが表示されず、以下のようなエラー画面に遷移してしまいます。
※エラー画面ではなく、入力画面に遷移し、エラーメッセージを表示させたいです。
Tyhmeleafに問題があるのかな?となんとなく思っていますが、どこに問題があるかがわからず。。。原因がお分かりの方がいましたら、教えて頂きたいです。

イメージ説明

SignupController.java

import com.example.application.service.UserApplicationService; import com.example.form.GroupOrder; import com.example.form.SignupForm; import lombok.extern.slf4j.Slf4j; @Controller @RequestMapping("/user") @Slf4j public class SignupController { @Autowired private UserApplicationService userApplicationService; @GetMapping("/signup") public String getSignup(Model model,@ModelAttribute SignupForm form ) { Map<String,Integer> genderMap = userApplicationService.getGenderMap(); model.addAttribute("genderMap",genderMap); return "user/signup"; } @PostMapping("/signup") public String postSignup(@Validated(GroupOrder.class) @ModelAttribute SignupForm form,Model model,BindingResult bindingResult) { if(bindingResult.hasErrors()) { return getSignup(model,form); } log.info(form.toString()); return "redirect:/login"; } }

###UserApplicationService.java

package com.example.application.service; import java.util.LinkedHashMap; import java.util.Locale; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.stereotype.Service; @Service public class UserApplicationService { @Autowired private MessageSource messageSource; public Map<String,Integer> getGenderMap(){ Map<String,Integer> genderMap = new LinkedHashMap<>(); String male=messageSource.getMessage("male", null,Locale.JAPAN); String female=messageSource.getMessage("female", null,Locale.JAPAN); genderMap.put(male, 1); genderMap.put(female, 2); return genderMap; } }

SignupForm.java

package com.example.form; import java.util.Date; import javax.validation.constraints.Email; import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import javax.validation.constraints.Pattern; import org.hibernate.validator.constraints.Length; import org.springframework.format.annotation.DateTimeFormat; import lombok.Data; @Data public class SignupForm { @NotBlank(groups=ValidGroup1.class) @Email(groups=ValidGroup2.class) private String userId; @NotBlank(groups=ValidGroup1.class) @Length(min=4,max=100,groups=ValidGroup2.class) @Pattern(regexp="^[a-zA-Z0-9]+$",groups=ValidGroup2.class) private String password; @NotBlank(groups=ValidGroup1.class) private String userName; @DateTimeFormat(pattern="yyyy/MM/dd") @NotNull(groups=ValidGroup1.class) private Date birthday; @Min(value=20,groups=ValidGroup1.class) @Max(value=100,groups=ValidGroup1.class) private Integer age; @NotNull(groups=ValidGroup1.class) private Integer gender; }

Validationグループ(計3クラス)

package com.example.form; import javax.validation.GroupSequence; @GroupSequence({ValidGroup1.class,ValidGroup2.class}) public interface GroupOrder { } package com.example.form; public interface ValidGroup1 { } package com.example.form; public interface ValidGroup2 { }

###messages.properties

user.signup.title=ユーザー登録 user.signup.btn=ユーザー登録 userId=ユーザーID password=パスワード userName=ユーザー名 birthday=誕生日 age=年齢 male=男性 female=女性 gender=性別

###ValidationMessages.properties

# ======================== # バインドエラーメッセージ # ======================== typeMismatch.signupForm.age=数値を入力してください typeMismatch.signupForm.birthday=yyyy/MM/dd形式で入力してください # ======================== # バリデーションエラーメッセージ # ======================== NotBlank={0}は必須入力です Email={0}はメールアドレス形式で入力してください Lendth={0}は{2}桁以上、{1}桁以下で入力してください Pattern={0}は半角英数字で入力してください NotNull={0}は必須入力です Min={0}は{1}以上を入力してください Max={0}は{1}以下を入力してください

Thymeleaf

<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"> <!-- CSS読み込み --> <link rel="stylesheet" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}"> <link rel="stylesheet" th;href="@{/css/user/signup.css}" type="text/css"> <!-- JS読み込み --> <script th:src="@{/webjars/jquery/jquery.min.js}"defer></script> <script th:src="@{/webjars/bootstrap/js/bootstrap.min.js}"defer></script> <title th:text="#{user.signup.title}"></title> </head> <body> <form id="signup-form" action="/user/signup" method="post" th:object="${signupForm}"> <h1 class="text-center" th:text="#{user.signup.title}"></h1> <!-- ユーザーID --> <div class="form-group"> <label for="userId" th:text="#{userId}"></label> <input type="text"class="form-control" th:field="*{userId}" th:errorclass="is-invalid"/> <div class="invalid-feedback" th:if="${#fields.hasErrors('userId')}" th:errors="*{userId}"></div> </div> <!-- パスワード --> <div class="form-group"> <label for="password" th:text="#{password}"></label> <input type="text"class="form-control"th:field="*{password}"th:errorclass="is-invalid"/> <div class="invalid-feedback" th:if="${#fields.hasErrors('password')}" th:errors="*{password}"></div> </div> <!-- ユーザー名 --> <div class="form-group"> <label for="userName" th:text="#{userName}"></label> <input type="text" class="form-control" th:field="*{userName}" th:errorclass="is-invalid"/> <div class="invalid-feedback" th:if="${#fields.hasErrors('userName')}" th:errors="*{userName}"></div> </div> <!-- 誕生日 --> <div class="form-group"> <label for="birthday" th:text="#{birthday}"></label> <input type="text" class="form-control" th:field="*{birthday}" th:errorclass="is-invalid"/> <div class="invalid-feedback" th:if="${#fields.hasErrors('birthday')}" th:errors="*{birthday}"></div> </div> <!-- 年齢 --> <div class="form-group"> <label for="age" th:text="#{age}"></label> <input type="text" class="form-control" th:field="*{age}" th:errorclass="is-invalid"/> <div class="invalid-feedback" th:if="${#fields.hasErrors('age')}" th:errors="*{age}"></div> </div> <!-- 性別 --> <div class="form-group"> <div th:each="item:${genderMap}" class="form-check-inline"> <input type="radio" th:value="${item.value}" th:text="${item.key}" class="form-check-input" th:field="*{gender}" th:errorclass="is-invalid"/> <div class="text-danger" th:if="${#fields.hasErrors('gender')}" th:errors="*{gender}"></div> </div> </div> <!-- 登録ボタン --> <input type="submit" th:value="#{user.signup.btn}" class="btn btn-primary w-100 mt-3"> </form> </body> </html>

補足情報

開発環境はEclipseを用いています。
ディレクトリ構造は以下です。
イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

Eclipseなら、ブレークポイントを設定することでそこで実行を止めることができ、変数のナカミを確認できます。
そこから1行づつ実行して、コードの流れを見れるようになります
そうやって、どこでおかしくなるのかを探っていきましょう

投稿2021/03/23 22:58

y_waiwai

総合スコア87774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問