前提
Thymeleafを用いてformタグを記述する中で、actionでリクエスト先を指定すると思いますが、
@{}をつけて記述すると、ページが見つかりませんと出てしまう。
実現したいこと
formタグ内から入力されたデータをコントローラーに送り、リダイレクトで別画面を表示させる。
発生している問題・エラーメッセージ
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Nov 05 18:19:12 JST 2022
There was an unexpected error (type=Not Found, status=404).
No message available
該当のソースコード
html
1<!-- action="@{/user/signup}にするとページが見当たりませんとでてしまう --> 2<form id="signup-form" method="post" action="/user/signup" class="form-signup" th:object="${signupForm}"> 3 <h1 class="text-center" th:text="#{user.signup.title}">ユーザー登録</h1> 4 <!-- ユーザーID --> 5 <div class="form-group"> 6 <label for="userId" th:text="#{userId}">ユーザーID</label> 7 <input type="text" class="form-control" th:field="*{userId}"> 8 </div> 9 <!-- パスワード --> 10 <div class="form-group"> 11 <label for="password" th:text="#{password}">パスワード</label> 12 <input type="text" class="form-control" th:field="*{password}"> 13 </div> 14 <!--1部省略--> 15 <!-- 登録ボタン --> 16 <input type="submit" th:value="#{user.signup.btn}" class="btn btn-primary w-100 mt-3"> 17 </form>
java
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.web.bind.annotation.GetMapping; 10import org.springframework.web.bind.annotation.ModelAttribute; 11import org.springframework.web.bind.annotation.PostMapping; 12import org.springframework.web.bind.annotation.RequestMapping; 13 14import com.example.application.service.UserApplicationService; 15import com.example.form.SignupForm; 16 17import lombok.extern.slf4j.Slf4j; 18 19@Controller 20@RequestMapping("/user") 21@Slf4j 22public class SignupController { 23 24 @Autowired 25 private UserApplicationService userApplicationService; 26 27 // 1部省略 28 29 // ユーザー登録処理 30 @PostMapping("/signup") 31 public String postSignup(@ModelAttribute SignupForm form) { 32 log.info(form.toString()); 33 return "redirect:/login"; // 別画面にリダイレクト 34 } 35} 36
対象のファイルの位置とパッケージエクスプローラー

回答1件
あなたの回答
tips
プレビュー