
前提・実現したいこと
spring bootでCRUDシステムを作っていますが、下記の例外が発生しているのでトラブルシュートをしたい。
発生している問題・エラーメッセージ
An error happened during template parsing (template: "class path resource [templates/persons/new.html]") org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/persons/new.html]") Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "#fields.hasErrors('name')" (template: "persons/new" - line 11, col 33) Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#fields.hasErrors('name')" (template: "persons/new" - line 11, col 33) Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'person' available as request attribute
該当のソースコード
newhtml
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="layout"> 3<head> 4 <title>New Person</title> 5</head> 6<body> 7 <div class="container" layout:fragment="content"> 8 <h1>New Person</h1> 9 <form th:action="@{/persons}" th:method="post" th:object="${person}"> 10 <div class="form-group" th:classappend="${#fields.hasErrors('name')}? has-error"> 11 <label class="control-label">名前</label> 12 <input class="form-control" type="text" th:field="*{name}" /> 13 <span class="text-danger" th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></span> 14 </div> 15 <div class="form-group" th:classappend="${#fields.hasErrors('age')}? has-error"> 16 <label class="control-label">年齢</label> 17 <input class="form-control" type="number" th:field="*{age}" /> 18 <span class="text-danger" th:if="${#fields.hasErrors('age')}" th:errors="*{age}"></span> 19 </div> 20 <div class="form-group" th:classappend="${#fields.hasErrors('belong')}? has-error"> 21 <label class="control-label">所属</label> 22 <input class="form-control" type="text" th:field="*{belong}" /> 23 <span class="text-danger" th:if="${#fields.hasErrors('belong')}" th:errors="*{belong}"></span> 24 </div> 25 <div class="form-group" th:classappend="${#fields.hasErrors('workplace')}? has-error"> 26 <label class="control-label">勤務地</label> 27 <input class="form-control" type="text" th:field="*{workplace}" /> 28 <span class="text-danger" th:if="${#fields.hasErrors('workplace')}" th:errors="*{workplace}"></span> 29 </div> 30 <button class="btn btn-default" type="submit">作成</button> 31 </form> 32 <div class="pull-right"> 33 <a class="btn btn-link" href="/persons">一覧画面へ</a> 34 </div> 35 </div> 36</body> 37</html>
controller
1package com.example.person.controller; 2 3 4 5import java.util.List; 6 7import javax.validation.Valid; 8 9import org.springframework.beans.factory.annotation.Autowired; 10import org.springframework.stereotype.Controller; 11import org.springframework.ui.Model; 12import org.springframework.validation.BindingResult; 13import org.springframework.web.bind.annotation.DeleteMapping; 14import org.springframework.web.bind.annotation.GetMapping; 15import org.springframework.web.bind.annotation.ModelAttribute; 16import org.springframework.web.bind.annotation.PathVariable; 17import org.springframework.web.bind.annotation.PostMapping; 18import org.springframework.web.bind.annotation.PutMapping; 19import org.springframework.web.bind.annotation.RequestMapping; 20 21import com.example.person.entity.PersonEntity; 22import com.example.person.service.PersonService; 23 24 25 26@Controller 27@RequestMapping("/persons") 28public class PersonController { 29 @Autowired 30 private PersonService personService; 31 32 @GetMapping 33 public String index(Model model) { 34 List<PersonEntity> person = personService.findAll(); 35 model.addAttribute("person", person); 36 return "persons/index"; 37 } 38 39 @GetMapping("new") 40 public String newPerson(Model model) { 41 PersonEntity person = new PersonEntity(); 42 model.addAttribute("person", person); 43 return "persons/new"; 44 } 45 46 @GetMapping("{id}/edit") 47 public String edit(@PathVariable int id, Model model) { 48 PersonEntity person = personService.findOne(id); 49 model.addAttribute("person",person); 50 return "persons/edit"; 51 } 52 53 @GetMapping("{id}") 54 public String show(@PathVariable int id, Model model) { 55 PersonEntity person = personService.findOne(id); 56 model.addAttribute("person", person); 57 return "persons/show"; 58 } 59 60 @PostMapping 61 public String create(@ModelAttribute PersonEntity person, BindingResult bindingResult) { 62 if(bindingResult.hasErrors()) return "persons/new"; 63 personService.save(person); 64 return "redirect:/persons"; 65 } 66 67 @PutMapping("{id}") 68 public String update(@PathVariable int id, @Valid @ModelAttribute PersonEntity person, BindingResult bindingResult) { 69 if(bindingResult.hasErrors()) return "persons/edit"; 70 person.setId(id); 71 personService.save(person); 72 return "redirect:/persons"; 73 } 74 75 @DeleteMapping("{id}") 76 public String destroy(@PathVariable int id) { 77 personService.delete(id); 78 return "redirect:/persons"; 79 } 80} 81
試したこと
Exception evaluating SpringEL expression: "#fields.hasErrors('name')" (template: "persons/new" - line 11, col 33)の記載がありましたのでhttps://teratail.com/questions/156753を参考に
"#fields.hasErrors('name')"やth:objectの見直しを行いました。
補足情報(FW/ツールのバージョンなど)
STS
phpmyadmin
回答2件
あなたの回答
tips
プレビュー