###画面遷移してかつパラメータを渡したい
Spring boot で以下の画面遷移で、ユーザ登録機能を作っています。
①登録画面→②確認画面→③完了画面
①から②へは、フォームで入力した値を渡すことができたのですが、
②から③へ、値を渡してDBへ登録したいのですができません。
###発生している問題・エラーメッセージ
②の画面で登録ボタン(③へPOST)を押下した時に、
②の画面で設定しているバリデーションが働き、すべての項目がNULLとなってしまう。
###ソースコード(コントローラとView2つ)
アカウントのコントローラ
java
1@Controller 2@RequestMapping("accounts") 3public class AccountController { 4 5 @Autowired 6 UserService userService; 7 8 @ModelAttribute 9 public AccountForm setupForm(){ 10 return new AccountForm(); 11 } 12 13 @RequestMapping(path = "create", method = RequestMethod.GET) 14 public String form(AccountForm accountForm, Model model) { 15 16 model.addAttribute("AccountForm", accountForm); 17 return "account/form"; 18 } 19 20 @RequestMapping(path = "confirm", method = RequestMethod.POST) 21 public String createConfirm(@Valid AccountForm accountForm, BindingResult result, Model model, 22 RedirectAttributes redirectAttributes){ 23 24 if (result.hasErrors()){ 25 return "account/form"; 26 } 27 model.addAttribute("AccountForm", accountForm); 28 return "account/confirm"; 29 } 30 31 @RequestMapping(path = "regist", method = RequestMethod.POST) 32 public String regist(@Valid AccountForm accountForm, BindingResult result, Model model){ 33 34 if (result.hasErrors()){ 35 return "account/confirm"; 36 } 37 38 model.addAttribute("AccountForm", accountForm); 39 userService.regist(accountForm); 40 return "account/regist"; 41 } 42 43}
①登録画面のView
html
1 2<!DOCTYPE html> 3<html xmlns="http://www.w3.org/1999/xhtml" 4 xmlns:th="http://www.thymeleaf.org"> 5 6 <head> 7 <title>登録画面</title> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 9 </head> 10 11 <body> 12 <h2>アカウント登録画面</h2> 13 <form action="#" th:action="@{/accounts/confirm}" th:object="${accountForm}" method="post"> 14 15 <p>メールアドレス: <input type="text" th:field="*{mail}" /> </p> 16 <span th:if="${#fields.hasErrors('*{mail}')}" th:errors="*{mail}" style="color: red" /> 17 18 <p>名前: <input type="text" th:field="*{name}" /></p> 19 <span th:if="${#fields.hasErrors('*{name}')}" th:errors="*{name}" style="color: red" /> 20 21 <p>パスワード : <input type="text" th:field="*{password}" /></p> 22 <p><input type="submit" value="登録" /> </p> 23 </form> 24 </body> 25</html>
②確認画面のソース(今回はパスワードは登録しないことにしています)
html
1<!DOCTYPE html> 2<html xmlns="http://www.w3.org/1999/xhtml" 3 xmlns:th="http://www.thymeleaf.org"> 4 5 <head> 6 <title>確認画面</title> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 8 </head> 9 10 <body> 11 <h2>確認画面</h2> 12 <form action="#" th:action="@{/accounts/regist}" th:object="${accountForm}" method="post"> 13 <p>この内容で登録しますか?</p> 14 15 <p th:text="'メール:' + ${accountForm.mail} " /> 16 <span th:if="${#fields.hasErrors('*{mail}')}" th:errors="*{mail}" style="color: red" /> 17 18 <p th:text="'氏名:' + ${accountForm.name}" /> 19 <span th:if="${#fields.hasErrors('*{name}')}" th:errors="*{name}" style="color: red" /> 20 21 <p><input type="submit" value="登録する"/></p> 22 23 <a href='/'>戻る</a> 24 </form> 25 </body> 26 27</html>

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/23 01:06