前提
SpringBootで従業員管理のシステムを作成しております。
従業員情報の更新処理でバリデーションが効いた際にリダイレクトで編集画面に戻りたいのですが、フォームに入力している値はth:fieldで取得できるのですが、プルダウンで選択していた値がth:selectedを使用しているため、th:fieldを使用できず、プルダウンで選択していた値を保持したまま編集画面にリダイレクトすることができません。
どう記述すればバリデーションをかけつつ値を保持してリダイレクトできるでしょうか。
プルダウンの中身はGETメソッドの際にデータベースから取得し、表示しております。
実現したいこと
- 従業員の更新処理でバリデーションが効いた際にフォーム・プルダウンの値を保持して編集画面にリダイレクトしたい
発生している問題・エラーメッセージ
エラーメッセージなし
該当のソースコード
Java
1@Controller 2public class EmpController { 3 4 @Autowired 5 private EmpService empService; 6 7 @Autowired 8 JdbcTemplate jdbcTemplate; 9 10 //編集画面の表示のGETメソッド 11 @GetMapping("/employeeDetail/{id}/edit") 12 public String displayEdit(@PathVariable("id") String id, Model model) { 13 //バリデーションエラーが出た場合データを保持した状態で表示する 14 if (!model.containsAttribute("employee")) { 15 Employee employee = empService.selectOne(id); 16 model.addAttribute("employee", employee); 17 } 18 19 //法人のプルダウンリスト表示 20 List<Company> company = empService.getCompanyAll(); 21 model.addAttribute("company", company); 22 23 return "edit"; 24 } 25 26 //更新処理のPOSTメソッド 27 @PostMapping("/employeeDetail/{id}/update") 28 public String postUpdate(@Validated Employee employee,BindingResult bindingResult,RedirectAttributes attributes,Model model,@PathVariable("id") String id) { 29 if (bindingResult.hasErrors()) { 30 employee = empService.selectOne(id); 31 attributes.addFlashAttribute("org.springframework.validation.BindingResult.employee", bindingResult); 32 attributes.addFlashAttribute("employee", employee); 33 34 //法人のプルダウンリスト表示 35 List<Company> company = empService.getCompanyAll(); 36 model.addAttribute("company", company); 37 38 return "edit"; 39 } 40 41 //従業員情報の更新処理(ここは正しく動く) 42 jdbcTemplate.update("update m_employee set name = ?,telephone_number = ?,mail_address = ?,company_id = ? where id = " + id, 43 employee.getName(), 44 employee.getTelephonenumber(), 45 employee.getMailaddress(), 46 employee.getCompany_id()); 47 48 return "redirect:/employeeDetail/{id}"; 49 } 50}
HTML
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head th:replace="header::head(~{::title})"> 4<meta charset="UTF-8"> 5<link th:href="@{/webjars/bootstrap/5.1.3/css/bootstrap.min.css}" rel="stylesheet"/> 6<title>従業員データベース</title> 7</head> 8<body> 9 <header th:replace="header::header"></header> 10 <div class="container"> 11 <div class="col-sm-5"> 12 <div class="page-header"> 13 <h2>従業員編集</h2> 14 </div> 15 <form method="post" th:object="${employee}" th:action="@{'/employeeDetail/' + ${employee.id} + '/update'}"> 16 <table class="table table-bordered table-hover"> 17 <tr> 18 <th class="active">氏名</th> 19 <td> 20 <div class="form-group"> 21 <input type="text" class="form-control" th:field="*{name}"></input> 22 <span class="errorStr" th:errors="*{name}"></span> 23 </div> 24 </td> 25 </tr> 26 <tr> 27 <th class="active">電話番号</th> 28 <td> 29 <div class="form-group"> 30 <input type="text" class="form-control" th:field="*{telephonenumber}"></input> 31 </div> 32 </td> 33 </tr> 34 <tr> 35 <th class="active">メールアドレス</th> 36 <td> 37 <div class="form-group"> 38 <input type="text" class="form-control" th:field="*{mailaddress}"></input> 39 </div> 40 </td> 41 </tr> 42 <tr> 43 <th class="active">法人名</th> 44 <td> 45 <div class="form-group"> 46 <select id="company" name="company_id"> 47 <option 48 th:each="company : ${company}" 49 th:value="${company.company_id}" 50 th:text="${company.company_name}" 51 th:selected="${company.company_name} == *{company_name}"> 52 </option> 53 </select> 54 </div> 55 </td> 56 </tr> 57 </table> 58 <button class="btn btn-primary" type="submit">更新</button> 59 </form> 60 </div> 61 </div> 62</body> 63</html>
Java
1@Entity 2@Data 3@Table(name="m_company") 4public class Company implements Serializable{ 5 6 @Id 7 @Column(name = "id", nullable = false) 8 private int company_id; 9 10 @Column(name = "company_name", nullable = false) 11 private String company_name; 12}
Java
1@Repository 2public interface CompanyRepository extends JpaRepository<Company, String>{ 3 4}
試したこと
編集画面のHTMLで<select>タグ内にth:fieldを書いてみたが、編集画面に遷移した際th:selectedの値が保持されなかった
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー