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

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

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

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

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Spring Boot

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

Q&A

0回答

751閲覧

spring bootでBindingResultを使用したエラーメッセージの表示が出来ない。

nissyan_hk

総合スコア5

Java

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

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Spring Boot

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

0グッド

0クリップ

投稿2022/10/09 03:18

実現したいこと

メールアドレスを使用した新規登録を行うときに
すでに登録されているメールアドレスがある場合
エラーメッセージを出す処理を行いたいと思っています。

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

error

1org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "access/signup" - line 19, col 28) 234・省略 5Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'SignUpForm' available as request attribute 678・省略

該当のソースコード(今回関係ない箇所は一部省略しています。)

view.html

1<body> 2<h1 class="mt-3">新規登録ページ</h1> 3 4<form action="" th:action="@{/signup/}" method="post" th:object="${SignUpForm}"> 5 <div class="mt-3"> 6 <label for="usernameInput" class="form-label">名前</label> 7 <input type="text" id="usernameInput" name="username" class="form-control"> 8 </div> 9 <div> 10 <input type="text" th:field="*{email}" /> 11 <span th:if="${#fields.hasErrors('email')}" th:errors="*{email}" style="color: red"></span> 12 </div> 13 <div class="mt-3"> 14 <label for="emailInput" class="form-label">メールアドレス</label> 15 <input type="text" id="emailInput" name="email" class="form-control"> 16 </div> 17 <div class="mt-3"> 18 <label for="passwordInput" class="form-label">パスワード</label> 19 <input type="password" id="passwordInput" name="password" class="form-control"> 20 </div> 21 <div class="mt-3" style="float:left;"> 22 <button type="submit" class="btn btn-primary">新規登録</button> 23 </div> 24</form> 25</body>

accessController.java

1@Controller 2@RequiredArgsConstructor 3public class AccessController { 4 5 private final AccessService accessService; 6 private final UserRepository userRepository; 7 private final UserService userService; 8 9 @PostMapping("/signup") 10 public String createSignupForm(@Validated @ModelAttribute SignUpForm suf, BindingResult result, 11Model model, HttpServletRequest request) { 12 13 //メールアドレスが登録されているか確認 14 Optional<User> registered = userRepository.findByUserEmail(suf.getEmail()); 15 model.addAttribute("SignUpForm", "test"); 16 17 // 登録されている場合、再び新規登録ページへ遷移しエラー表示 18 if(registered != null) { 19 result.rejectValue("SignUpForm", "email.duplicated"); 20 return createValidationErrorResponse(); 21 } 22 23 24 userService.create(suf.getUsername(), suf.getEmail(), suf.getPassword(), "USER"); 25 26 SecurityContext context = SecurityContextHolder.getContext(); 27 Authentication authentication = context.getAuthentication(); 28 29 if (authentication instanceof AnonymousAuthenticationToken == false) { 30 SecurityContextHolder.clearContext(); 31 } 32 33 try { 34 request.login(suf.getEmail(), suf.getPassword()); 35 } catch (ServletException e) { 36 e.printStackTrace(); 37 } 38 39 return "redirect:/"; 40 } 41 42 private String createValidationErrorResponse() { 43 //新規登録ページへ遷移 44 return "access/signup"; 45 } 46} 47

SignUpForm.java

1@Data 2@AllArgsConstructor 3public class SignUpForm { 4 5 @NotBlank(message = "名前を入力してください。") 6 @UniqueUsername 7 private String username; 8 9 @NotBlank(message = "メールアドレスを入力してください。") 10 @Email(message = "@を付けたアドレスを入力してください。") 11 private String email; 12 13 @NotBlank(message = "12桁〜128桁までのパスワードを入力してください。") 14 @Size(min=12, max = 128) 15 private String password; 16 17 private String authority; 18 19}

message.properties(階層はapplication.propertiesと同じ階層です。)

1email.duplicated=入力されたメールアドレスは登録済みです。

以上、ご教授お願い致します。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問