Spring Bootを用いてwebアプリの開発をしています。
ログイン機能よりログインをしたユーザーのプロフィールの確認、
プロフィールの編集機能を作っています。
プロフィールの編集機能を完了後にプロフィール確認画面へリダイレクトをするのですが
情報の反映ができません。
しかしながらDBでは情報の更新ができているという状態になっています。
再度アプリケーションを立ち上げると情報の反映はされるのですが、
リダイレクトしてすぐには反映がされないのは何故でしょうか?
アドバイス等をいただけると幸いです。よろしくお願いいたします。
java
1@Controller 2public class ProfileController { 3 @Autowired 4 UserService userService; 5 6 @Autowired 7 UserRepository userRepository; 8 9 @ModelAttribute 10 public UserProfileForm userProfileForm() { 11 return new UserProfileForm(); 12 } 13 14 @GetMapping("/profile/{userId}") 15 String showProfile() { 16 return "profile/userProfile"; 17 } 18 19 20 @GetMapping("/profile/edit/{userId}") 21 String showProfileEdit() { 22 return "profile/userEdit"; 23 } 24 25 @PostMapping(value="/profile/edit/{userId}") 26 String create(@Validated UserProfileForm userForm, 27 BindingResult bindingResult, 28 @PathVariable("userId") Integer userId, 29 Model model) { 30 if(bindingResult.hasErrors()) { 31 return "profile/userEdit"; 32 } 33 User user = userRepository.findById(userId).orElseGet(null); 34 user.setUserName(userForm.getUserName()); 35 user.setGender(userForm.getGender()); 36 user.setAge(userForm.getAge()); 37 user.setSelfIntroduction(userForm.getSelfIntroduction()); 38 userService.update(user); 39 return "redirect:/profile/"+userId; 40 } 41
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org" 3 xmlns:sec="http://www.thymeleaf.org/extras/spring-security"> 4<head> 5<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 6<title>Insert title here</title> 7</head> 8<body th:with="user = ${#authentication.principal.user}"> 9 <h1>プロフィール画面</h1> 10 <p>ユーザー名:<span th:text="${user.userName}"></span></p> 11 <p>性別:<span th:text="${user.gender}"></span></p> 12 <p>年齢:<span th:text="${user.age}"></span></p> 13 <p>自己紹介:<span th:text="${user.selfIntroduction}"></span></p> 14 <a th:href="@{'/profile/edit/'+${user.userId}}">プロフィールを編集する</a> 15 <a th:href="@{'/profile/studentEdit/'+${user.userId}}">生徒情報を編集する</a> 16</body> 17</html>
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org" 3 xmlns:sec="http://www.thymeleaf.org/extras/spring-security"> 4<head> 5<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 6<title>Insert title here</title> 7</head> 8<body th:with="user = ${#authentication.principal.user}"> 9 <h1>プロフィール編集画面</h1> 10 <table> 11 <form th:action="@{'/profile/edit/'+${user.UserId}}" th:object="${userProfileForm}" method="post"> 12 <tr><td><label for="userName">ユーザ名:</label></td> 13 <td><input type="text" name="userName" th:attr="value = ${user.userName}" th:errorclass="err"/> 14 <div class="err" th:each="err,st : ${#lists.sort(#fields.errors('userName'))}" 15 th:text="${err}" th:if="${st.index == 0}">エラーメッセージが出力されます</div> 16 </td> 17 </tr> 18 <tr> 19 <td><label for="gender">性別</label></td> 20 <td><input type="radio" name="gender" th:field="*{gender}" id="radioA" value="男性"/> 21 <label for="radioA">男性</label> 22 <input type="radio" name="gender" th:field="*{gender}" id="radioB" value="女性"/> 23 <label for="radioB">女性</label> 24 </td> 25 </tr> 26 <tr> 27 <td><label for="age">年齢:</label></td> 28 <td><select id="age" name="age" th:errorclass="err"> 29 <option th:each="i : ${#numbers.sequence(0, 100)}" 30 th:value="${i}" th:text="${i}" th:selected="${i==user.age}">歳 31 </option> 32 </select> 33 <div class="err" th:each="err,st : ${#lists.sort(#fields.errors('age'))}" 34 th:text="${err}" th:if="${st.index == 0}">エラーメッセージが出力されます</div> 35 </td> 36 </tr> 37 <tr> 38 <tr> 39 <td><label for="selfIntroduction">自己紹介</label></td> 40 <td><textarea name="selfIntroduction" th:errorclass="err" th:text="${user.selfIntroduction}"> 41 </textarea> 42 <div class="err" th:each="err,st : ${#lists.sort(#fields.errors('selfIntroduction'))}" 43 th:text="${err}" th:if="${st.index == 0}">エラーメッセージが出力されます</div> 44 </td> 45 </tr> 46 <tr> 47 <td><a th:href="@{'/profile/'+${user.userId}}">プロフィール画面に戻る</a></td> 48 <td> 49 <input type="submit" value="プロフィールを変更" /> 50 </td> 51 </tr> 52 </form> 53 </table> 54 55</body> 56</html>
##プロフィール画面
##プロフィール編集画面(自己紹介等を編集)
##編集後にプロフィール画面にリダイレクト(情報が反映されない,DBへ情報は反映されている)
spring Boot2.2.4
mysql 8.0.18
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。