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

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

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

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

Webサーバー

Webサーバーとは、HTTPリクエストに応じて、クライアントに情報を提供するシステムです。

バリデーション

Validationとは特定の入力データが、求められた条件に当てまっているかをチェックするために使われます。

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

Spring Boot

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

Q&A

解決済

1回答

2788閲覧

SpringBootでバリデーションが機能しない

aokiYosuke

総合スコア2

Java

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

Webサーバー

Webサーバーとは、HTTPリクエストに応じて、クライアントに情報を提供するシステムです。

バリデーション

Validationとは特定の入力データが、求められた条件に当てまっているかをチェックするために使われます。

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

Spring Boot

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

0グッド

0クリップ

投稿2021/07/14 06:13

前提・実現したいこと

SpringBootを利用して以下書籍を参考にしながら、サンプルWebアプリケーションを作成しているのですが、バリデーションがうまく作動せず、空白文字で遷移してもエラーメッセージを出力してくれません。
アノテーションを読み込めていないのかなと思っているのですが、原因がわからずお力貸していただけませんでしょうか。
参考書籍:https://www.amazon.co.jp/%E5%BE%8C%E6%82%94%E3%81%97%E3%81%AA%E3%81%84%E3%81%9F%E3%82%81%E3%81%AESpring-Boot-%E5%85%A5%E9%96%80%E6%9B%B8%EF%BC%9ASpring-%E8%A7%A3%E4%BD%93%E6%96%B0%E6%9B%B8%EF%BC%88%E7%AC%AC2%E7%89%88%EF%BC%89Spring-Boot%E3%81%8C%E4%B8%B8%E5%88%86%E3%81%8B%E3%82%8A/dp/4802092865/ref=pd_sbs_5/356-0278638-6666779?pd_rd_w=rxjRj&pf_rd_p=367c54b8-500b-4071-9b4d-65fe16192688&pf_rd_r=0KA5Z16ZFCX9895224E9&pd_rd_r=b2f7ab7a-bfb1-4394-9d20-431346055658&pd_rd_wg=DeGyN&pd_rd_i=4802092865&psc=1

発生している問題・エラーメッセージ

・入力フォームに空白もしくはNullが入力されている際に、バリテーションでエラーメッセージを出力したいが、うまく機能せず、正常稼働時と同様の画面遷移になってしまう。

該当のソースコード

pom

1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.5.2</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.example</groupId> 12 <artifactId>SpringBootSample</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>SpringBootSample</name> 15 <description>Demo project for Spring Boot</description> 16 <properties> 17 <java.version>11</java.version> 18 </properties> 19 <dependencies> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-data-jdbc</artifactId> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-jdbc</artifactId> 27 </dependency> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-thymeleaf</artifactId> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-web</artifactId> 35 </dependency> 36 <dependency> 37 <groupId>org.springframework.boot</groupId> 38 <artifactId>spring-boot-devtools</artifactId> 39 <scope>runtime</scope> 40 <optional>true</optional> 41 </dependency> 42 <dependency> 43 <groupId>com.h2database</groupId> 44 <artifactId>h2</artifactId> 45 <scope>runtime</scope> 46 </dependency> 47 <dependency> 48 <groupId>org.projectlombok</groupId> 49 <artifactId>lombok</artifactId> 50 <optional>true</optional> 51 </dependency> 52 <dependency> 53 <groupId>org.springframework.boot</groupId> 54 <artifactId>spring-boot-starter-test</artifactId> 55 <scope>test</scope> 56 </dependency> 57 <!-- query --> 58 <dependency> 59 <groupId>org.webjars</groupId> 60 <artifactId>jquery</artifactId> 61 <version>3.5.1</version> 62 </dependency> 63 <!-- bootstrap --> 64 <dependency> 65 <groupId>org.webjars</groupId> 66 <artifactId>bootstrap</artifactId> 67 <version>4.5.3</version> 68 </dependency> 69 <!-- webjars-locator --> 70 <dependency> 71 <groupId>org.webjars</groupId> 72 <artifactId>webjars-locator</artifactId> 73 <version>0.40</version> 74 </dependency> 75 <!-- validator --> 76 <dependency> 77 <groupId>org.springframework.boot</groupId> 78 <artifactId>spring-boot-starter-validation</artifactId> 79 </dependency> 80 </dependencies> 81 82 <build> 83 <plugins> 84 <plugin> 85 <groupId>org.springframework.boot</groupId> 86 <artifactId>spring-boot-maven-plugin</artifactId> 87 <configuration> 88 <excludes> 89 <exclude> 90 <groupId>org.projectlombok</groupId> 91 <artifactId>lombok</artifactId> 92 </exclude> 93 </excludes> 94 </configuration> 95 </plugin> 96 </plugins> 97 </build> 98 99</project>

Form

1package com.example.form; 2 3import java.util.Date; 4 5import javax.validation.constraints.Email; 6import javax.validation.constraints.Max; 7import javax.validation.constraints.Min; 8import javax.validation.constraints.NotBlank; 9import javax.validation.constraints.NotNull; 10import javax.validation.constraints.Pattern; 11 12import org.hibernate.validator.constraints.Length; 13import org.springframework.format.annotation.DateTimeFormat; 14 15import lombok.Data; 16 17@Data 18public class SignupForm { 19 20 @NotBlank 21 @Email 22 private String userId; 23 24 @NotBlank 25 @Length(min=4,max=100) 26 @Pattern(regexp="^[a-zA-Z0-9]+$") 27 private String password; 28 29 @NotBlank 30 private String userName; 31 32 @DateTimeFormat(pattern = "yyyy/MM/dd") 33 @NotNull 34 private Date birthday; 35 36 @Min(20) 37 @Max(100) 38 private Integer age; 39 40 @NotNull 41 private Integer gender; 42 43}

Controller

1package com.example.controller; 2 3import java.util.Locale; 4import java.util.Map; 5 6import org.springframework.beans.factory.annotation.Autowired; 7import org.springframework.stereotype.Controller; 8import org.springframework.ui.Model; 9import org.springframework.validation.BindingResult; 10import org.springframework.validation.annotation.Validated; 11import org.springframework.web.bind.annotation.GetMapping; 12import org.springframework.web.bind.annotation.ModelAttribute; 13import org.springframework.web.bind.annotation.PostMapping; 14import org.springframework.web.bind.annotation.RequestMapping; 15 16import com.example.application.service.UserApplicationService; 17import com.example.form.SignupForm; 18 19import lombok.extern.slf4j.Slf4j; 20 21@Controller 22@RequestMapping("/user") 23@Slf4j 24public class SignupController { 25 @Autowired 26 private UserApplicationService userApplicationService; 27 28 //** ユーザ登録画面を表示 */ 29 @GetMapping("signup") 30 public String getSignup(Model model,Locale locale,@ModelAttribute SignupForm form) { 31 //性別を取得 32 Map<String,Integer> genderMap=userApplicationService.getGenderMap(locale); 33 model.addAttribute("genderMap",genderMap); 34 35 //ユーザ登録画面に遷移 36 return "user/signup"; 37 } 38 39 /** ユーザ登録処理 */ 40 @PostMapping("/signup") 41 public String postSignup(Model model,Locale locale,@ModelAttribute @Validated SignupForm form, BindingResult bindingResult) { 42 43 //入力チェック結果 44 if(bindingResult.hasErrors()) { 45 //NG:ユーザ登録画面に戻ります 46 return getSignup(model,locale,form); 47 } 48 log.info("has error: {}", bindingResult.getFieldError()); 49 log.info(form.toString()); 50 51 // ログイン画面にリダイレクト 52 return "redirect:/login"; 53 } 54} 55

html

1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <meta charset="UTF-8"></meta> 5 <meta name="viewpoint" content="width=device-width,initial-scale=1,shrink-to-fit=no"> 6 <!-- CSS読込 --> 7 <link rel="stylesheet" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}"> 8 <link rel="stylesheet" th:href="@{/css/user/signup.css}"> 9 <!-- JS読込 --> 10 <script th:src="@{/webjars/jquery/jquery.min.js}" defer></script> 11 <script th:src="@{/webjars/bootstrap/bootstrap.min.js}" defer></script> 12 <title th:text="#{user.signup.title}"></title> 13</head> 14<body class="bg-light"> 15 <form id="signup-form" method="post" action="/user/signup" class="form-signup" th:object="${signupForm}"> 16 <h1 class="text-center" th:text="#{user.signup.title}"></h1> 17 <!-- ユーザID --> 18 <div class="form-group"> 19 <label for="userId" th:text="#{userId}"></label> 20 <input type="text" class="form-control" th:field="*{userId}" th:errorclass="is-invalid"/> 21 <div class="invalid-feedback" th:errors="*{userId}"></div> 22 </div> 23 <!-- パスワード --> 24 <div class="form-group"> 25 <label for="password" th:text="#{password}"></label> 26 <input type="text" class="form-control" th:field="*{password}" th:errorclass="is-invalid"/> 27 <div class="invalid-feedback" th:errors="*{password}"></div> 28 </div> 29 <!-- ユーザ名 --> 30 <div class="form-group"> 31 <label for="userName" th:text="#{userName}"></label> 32 <input type="text" class="form-control" th:field="*{userName}" th:errorclass="is-invalid"/> 33 <div class="invalid-feedback" th:errors="*{userName}"></div> 34 </div> 35 <!-- 誕生日 --> 36 <div class="form-group"> 37 <label for="birthday" th:text="#{birthday}"></label> 38 <input type="text" placeholder="yyyy/mm/dd" class="form-control" th:field="*{birthday}" th:errorclass="is-invalid"/> 39 <div class="invalid-feedback" th:errors="*{birthday}"></div> 40 </div> 41 <!-- 年齢 --> 42 <div class="form-group"> 43 <label for="age" th:text="#{age}"></label> 44 <input type="text" class="form-control" th:field="*{age}" th:errorclass="is-invalid"/> 45 <div class="invalid-feedback" th:errors="*{age}"></div> 46 </div> 47 <!-- 性別 --> 48 <div class="form-group"> 49 <div th:each="item:${genderMap}" class="form-check-inline"> 50 <input type="radio" class="form-check-input" th:value="${item.value}" th:field="*{gender}" th:errorclass="is-invalid"/> 51 <label class="form-check-label" th:text="${item.key}"></label> 52 <div class="text-danger" th:if="${#fields.hasErrors('gender')}" th:errors="*{gender}"></div> 53 </div> 54 </div> 55 <!-- 登録ボタン --> 56 <input type="submit" th:value="#{user.signup.btn}" class="btn btn-primary w-100 mt-3" /> 57 </form> 58</body> 59</html>

試したこと

・log.infoの結果を見ていると、引数として空白もしくはnullが渡されているので、判定がうまくいっていない(アノテーションが効いていない?)という推測です。

補足情報(FW/ツールのバージョンなど)

Java 11
SpringBoot 2.1.15
以下Gitにもコードを置いております。
https://github.com/aokiYosuke/Signup.git

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

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

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

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

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

guest

回答1

0

自己解決

Webサーバ再起動したら解決しました...
SpringBootって再起動なしでもセーブで更新ができると思っていたのですが...
失礼いたしました。

投稿2021/07/14 06:23

aokiYosuke

総合スコア2

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

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

m.ts10806

2021/07/14 06:26

ホットデプロイが有効になっているか確認してください。 STS(eclipse含む)ならプロジェクトにdevtoolsを付けるといけるはずです。
Luice

2021/07/14 06:29

Javaはスクリプト言語ではないので、アプリケーションサーバーを再起動して はじめて変更が反映されます。 spring-boot-devtoolsを使用するとビルド-デプロイ-再起動の手間を簡略化する事が出来ます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問