前提・実現したいこと
springbootでwebアプリケーションの勉強中です。
あらかじめフォームで送った内容を編集・更新する機能を実装しているのですが、エラーが出て困っています。
編集・更新する際に、null禁止などのバリデーションをかけているのですが、バリデーションに引っかかった場合は、元の編集画面に戻し、エラーメッセージを出したいです。
現在、編集画面でフォームを編集、バリデーションに引っ掛かった場合、更新ボタンを押すとエラーが出てしまいます。
バリデーションに引っかからない場合は問題なく編集ができ、ページ遷移します。
発生している問題・エラーメッセージ
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/todo/edit.html]")
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "todo.id" (template: "todo/edit" - line 15, col 11)
該当のソースコード
Controller
1@GetMapping("todo/{id}/edit") 2 public ModelAndView edit(@PathVariable("id") Long id, ModelAndView mav){ 3 4 Optional<Todo> todoOne = todoService.findById(id); 5 if(todoOne.isPresent()){ 6 Todo todo = todoOne.get(); 7 mav.addObject("todo",todo); 8 mav.setViewName("todo/edit"); 9 }else { 10 mav.addObject("error","該当するToDoはありません"); 11 mav.setViewName("todo/edit"); 12 } 13 return mav; 14 } 15 16@PostMapping("/todo/{id}/edit") 17 public ModelAndView updateTodo(@ModelAttribute @Validated TodoValid todoValid, BindingResult result, @PathVariable("id") Long id, RedirectAttributes redirectAttributes,ModelAndView mav){ 18 Todo todo = todoService.getById(id); 19 20 if (result.hasErrors()) { 21 mav.setViewName("todo/edit"); 22 return mav; 23 } 24 25(中略) 26 27 BeanUtils.copyProperties(todoValid, todo); 28 todoService.save(todo); 29 mav.setViewName("redirect:/"); 30 return mav; 31 }
バリデーション設定↓
TodoValid
1package com.example.web.controller; 2 3import org.hibernate.validator.constraints.Length; 4 5import org.springframework.format.annotation.DateTimeFormat; 6import javax.validation.constraints.NotNull; 7 8import java.util.Date; 9 10public class TodoValid { 11 12 @Length(max = 30,message = "Todoは30字以内で入力してください") 13 @NotNull(message = "Todoを入力してください") 14 private String text; 15 16 @NotNull(message = "期限を設定してください") 17 @DateTimeFormat(pattern = "yyyy-MM-dd") 18 private Date limittime; 19 20 public String getText() { 21 return text; 22 } 23 24 public void setText(String text) { 25 this.text = text; 26 } 27 28 public Date getLimittime() { 29 return limittime; 30 } 31 32 public void setLimittime(Date limittime) { 33 this.limittime = limittime; 34 } 35 36 37} 38
エンティティ
Todo
1@Entity 2@Table(name = "todo") 3public class Todo { 4 5 @Id 6 @GeneratedValue(strategy = GenerationType.AUTO) 7 private Long id; 8 9 private String text; 10 11 @DateTimeFormat(pattern = "yyyy-MM-dd") 12 private Date limittime; 13 14 @DateTimeFormat(pattern = "yyyy-MM-dd") 15 @Column(updatable = false) 16 private Date createtime; 17 18 private boolean complete; 19 20(ゲッター、セッター省略)
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <meta charset="UTF-8" /> 5 <link rel="stylesheet" type="text/css" href="/css/style.css" th:href="@{/css/style.css}" /> 6 <title>Todo編集</title> 7</head> 8<body> 9<header> 10 <a class="logo" th:href="@{'/'}">ToDoリスト管理</a> 11 <div class="serch"><a th:href="@{'/todo' + '/find'}">検索</a></div> 12</header> 13<div class="new"> 14 <p>ToDoの内容を変更します</p> 15 <form th:action="@{'/todo/' + ${todo.id} + '/edit'}" method="post" th:object="{todoValid}"> 16 17 <div th:object="${todo}"> 18 <div>ToDo名 19 <textarea placeholder="すること" name="text" th:text="*{text}" maxlength="30"></textarea> 20 </div> 21 <div>期限 22 <input name="limittime" type="date" th:value="*{#dates.format(createtime,'yyyy-MM-dd')}"/> 23 </div> 24 25 <input class="button" type="submit" value="ToDoの更新" name="new" /> 26 27 </div> 28 29 <div th:each="todoValid:${todoValid}"> 30 <div th:if="${#fields.hasErrors('text')}" th:errors="*{text}"></div> 31 <div th:if="${#fields.hasErrors('limittime')}" th:errors="*{limittime}"></div> 32 </div> 33 34 <div th:if="${error}" th:text="${error}"></div> 35 36 </form> 37 38</div> 39 40</body> 41</html>
###その他
エラーが出ている箇所は「edit.hrml」の
問題の箇所がわかる方がいらっしゃったら、ご教授いただきたいです。
補足情報(FW/ツールのバージョンなど)
・Springboot(gradle)
・thymeleaf
・MySQL@5.7
・intelliJ
回答1件
あなたの回答
tips
プレビュー