如何使用thymelaf修复表单提交模板中的500状态错误



我使用Spring文档编写了一些代码:https://spring.io/guides/gs/handling-form-submission/

我想我做了和文档中相同的事情,但我的代码返回了我(类型=内部服务器错误,状态=500(。

这是我的代码:

FriendListsModel.java

@Entity
public class FriendListsModel {
@Id
private Integer id;
private String friendLists;
// constructor, getters, setters, equal, hashcode, toString (all autogenerated)

addController.java

@Controller
public class addController {
@GetMapping("/add2")
public String addForm(Model model) {
model.addAttribute("form", new FriendListsModel());
return "form";
}
@PostMapping("/add2")
public String addSubmit(@ModelAttribute FriendListsModel friendListsModel, Model model) {
model.addAttribute("form", friendListsModel);
return "result";
}
}

form.html

<!DOCTYPE HTML>
<html xmlns:th="https://www.thymeleaf.org">
<head> 
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Form</h1>
<form action="#" th:action="@{/add2}" th:object="${form}" method="post">
<p>Id: <input type="text" th:field="*{id}" /></p>
<p>Message: <input type="text" th:field="*{friendLists}" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>

result.html

<!DOCTYPE HTML>
<html xmlns:th="https://www.thymeleaf.org">
<head> 
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Result</h1>
<p th:text="'id: ' + ${form.id}" />
<p th:text="'content: ' + ${form.friendLists}" />
<a href="/add2">Submit another message</a>
</body>
</html>

日志:

Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'friendLists' of bean class [com.hoff.blog.FriendListsModel]: Bean property 'friendLists' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
2022-01-05 20:23:17.347 DEBUG 16504 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Failed to complete request: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "form" - line 11, col 40)     
2022-01-05 20:23:17.358 ERROR 16504 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "form" - line 11, col 40)] with root cause
2022-01-05 20:23:17.388 DEBUG 16504 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
2022-01-05 20:23:17.391 DEBUG 16504 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2022-01-05 20:23:17.412 DEBUG 16504 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2022-01-05 20:23:17.414 DEBUG 16504 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 500

问题出现在我的模型中,我之前刚刚更改了字段名称,忘记更改getter和setter名称。

相关内容

最新更新