以下のようにsubmitを実行しようとしましたが画面遷移もされず、
DBへの情報の反映がされません。
action属性なども確認をしてみたのが何が問題なのかがわかりませんでした。
何かアドバイスをいただけると幸いです。
java
1package tutors.app.profile.controller; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.stereotype.Controller; 5import org.springframework.ui.Model; 6import org.springframework.validation.BindingResult; 7import org.springframework.validation.annotation.Validated; 8import org.springframework.web.bind.annotation.GetMapping; 9import org.springframework.web.bind.annotation.ModelAttribute; 10import org.springframework.web.bind.annotation.PathVariable; 11import org.springframework.web.bind.annotation.PostMapping; 12import org.springframework.web.servlet.mvc.support.RedirectAttributes; 13 14import tutors.app.profile.form.*; 15import tutors.domain.model.Region; 16import tutors.domain.model.Student; 17import tutors.domain.model.Subject; 18import tutors.domain.model.Teacher; 19import tutors.domain.model.User; 20import tutors.domain.repository.RegionRepository; 21import tutors.domain.repository.StudentRepository; 22import tutors.domain.repository.SubjectRepository; 23import tutors.domain.repository.TeacherRepository; 24import tutors.domain.repository.UserRepository; 25import tutors.domain.service.student.*; 26import tutors.domain.service.teacher.TeacherService; 27import tutors.domain.service.user.UserService; 28 29import java.time.LocalTime; 30import java.util.ArrayList; 31import java.util.List; 32import java.util.Map; 33import java.util.stream.Collectors; 34import java.util.stream.Stream; 35 36@Controller 37public class ProfileController { 38 @Autowired 39 UserService userService; 40 @Autowired 41 StudentService studentService; 42 @Autowired 43 TeacherService teacherService; 44 @Autowired 45 UserRepository userRepository; 46 @Autowired 47 RegionRepository regionRepository; 48 @Autowired 49 SubjectRepository subjectRepository; 50 @Autowired 51 StudentRepository studentRepository; 52 @Autowired 53 TeacherRepository teacherRepository; 54 55 @ModelAttribute 56 public UserEditForm userProfileForm() { 57 return new UserEditForm(); 58 } 59 @ModelAttribute 60 public StudentEditForm studentProfileForm() { 61 return new StudentEditForm(); 62 } 63 @ModelAttribute 64 public TeacherEditForm teacherProfileForm() { 65 return new TeacherEditForm(); 66 } 67 68 69 70//-----------user profile編集--------------// 71 72 @GetMapping("/profile/edit/{userId}") 73 String showProfileEdit(@PathVariable("userId") Integer userId, 74 Model model) { 75 User user = userRepository.findById(userId).orElseGet(null); 76 List<Region> regionList = regionRepository.findAll(); 77 model.addAttribute("regionList",regionList); 78 model.addAttribute("user",user); 79 return "profile/userEdit"; 80 } 81 82 //--------------教師プロフィール編集------------------// 83 84 @GetMapping("/profile/teacherEdit/{userId}") 85 String showTeacherProfileEdit(@PathVariable("userId") Integer userId,Model model) { 86 Teacher teacher = teacherRepository.findById(userId).orElse(null); 87 TeacherEditForm teacherEditForm = new TeacherEditForm(); 88 model.addAttribute("teacherEditForm",teacherEditForm); 89 return "profile/teacherEdit"; 90 } 91 92 @PostMapping("/profile/teacherEdit/{userId}") 93 String editTeacher(@Validated TeacherEditForm teacherEditForm, 94 BindingResult bindingResult, 95 RedirectAttributes attributes, 96 @PathVariable("userId") Integer userId, 97 Model model) { 98 if(bindingResult.hasErrors()) { 99 return "profile/teacherEdit"; 100 } 101 TeacherEditForm teacherEditForm = new TeacherEditForm(); 102 teacherService.createteacher(userId, teacherEditForm.getMinWage(), teacherEditForm.getPolicy()); 103 attributes.addFlashAttribute("teacherMsg", "※教師情報を更新しました。"); 104 return "redirect:/profile/{userId}"; 105 } 106}
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 <form th:action="@{'/profile/teacherEdit/'+${user.userId}}" 11 th:object="${teacherEditForm}" method="post"> 12 <label for="minWage">希望する時給:</label> <select id="minWage" 13 th:field="*{minWage}" th:errorclass="err"> 14 <option value="-1" selected>選択してください</option> 15 <option th:each="i : ${#numbers.sequence(1000, 10000,100)}" 16 th:value="${i}" th:text="${i}+'円〜'" 17 th:selected="${i==teacher?.minWage}"></option> 18 </select> 19 <div class="err" 20 th:each="err,st : ${#lists.sort(#fields.errors('minWage'))}" 21 th:text="${err}" th:if="${st.index == 0}">エラーメッセージが出力されます</div> 22 <hr /> 23 24 <p>指導方針(最大300文字)</p> 25 <textarea name="policy" th:errorclass="err" th:text="${teacher?.policy}"></textarea> 26 <div class="err" 27 th:each="err,st : ${#lists.sort(#fields.errors('policy'))}" 28 th:text="${err}" th:if="${st.index == 0}">エラーメッセージが出力されます 29 </div> 30 <div> 31 <input type="submit" value="プロフィールを変更" /> 32 </div> 33 <div> 34 <a th:href="@{'/profile/'+${user.userId}}">プロフィール画面に戻る</a> 35 </div> 36 </form> 37</body> 38</html>
回答2件
あなたの回答
tips
プレビュー