Spring bootにて簡単なCRUDができるアプリ を作成したいです。
ユーザ登録画面にてフォーム情報をバインディングしControllerへ送るためにth:objectを使っています。
その中でフィールドとするものに対して
th:field="*{username}"
のような形で指定していますが、500エラーがかえってきます。
エラーの内容としては以下が主因かと思っています。
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "index" - line 19, col 67)
index.htmlのline19というのは以下の**th:field="*{username}"**の部分になります。
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Title</title> </head> <body onload="document.login.email.focus();"> <!-- ログインが失敗した時のメッセージ --> <div> <h1>index</h1> <form th:action="@{/account/create}" method="post" th:object="${accountEntity}"> <div class="form-group"> <label class="control-label">name</label> <input id="username" type="text" class="form-control" th:field="*{username}" name="username"> <span th:if="${#fields.hasErrors('username')}" th:errors="*{username}">error</span><br/> </div> <div class="form-group"> <label class="control-label">name</label> <input id="email" type="text" class="form-control" th:field="*{email}" name="email"> <span th:if="${#fields.hasErrors('email')}" th:errors="*{email}">error</span><br/> </div> <div class="form-group"> <label class="control-label">name</label> <input id="password" type="text" class="form-control" th:field="*{password}" name="password"> <span th:if="${#fields.hasErrors('password')}" th:errors="*{password}">error</span><br/> </div> <button class="btn btn-default" type="submit">作成</button> </form> </div> <!-- ログインのフォーム --> </body> </html>
objectとしての指定が間違っているからエラーになっているのかと思っております。
objectとして指定しているクラスが以下の**insertAccount()**の部分です。変数名をそのままindex.htmlで設定しております。
package com.example.security.controller; import com.example.security.entity.AccountEntity; import com.example.security.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.List; @Controller @RequestMapping("account") public class AccountController { @Autowired AccountService accountService; @RequestMapping(method = RequestMethod.GET) List<AccountEntity> getAccount(){ return accountService.findAll(); } @RequestMapping(method = RequestMethod.POST) String insertAccount(@Valid @ModelAttribute AccountEntity accountEntity, BindingResult result){ if (result.hasErrors()) { return "index"; } accountService.save(accountEntity); return "finish"; } @RequestMapping(value = "{id}",method = RequestMethod.DELETE) void deleteAccount(@PathVariable("id") Integer id){ accountService.delete(id); } }
th:objectの指定で間違いがありますでしょうか?
それともfieldの指定に間違いがあるでしょうか?
ちなみにEntityは以下のような定義です。
ご意見をよろしくお願いします。
package com.example.security.entity; import lombok.Data; import javax.persistence.*; import javax.validation.constraints.Email; import javax.validation.constraints.Max; import javax.validation.constraints.Min; @Entity @Table(name = "account") @Data public class AccountEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column @Min(4) @Max(19) private String username; @Column @Email private String email; @Column private String password; @Column private boolean admin; }
あなたの回答
tips
プレビュー