我使用 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";
}
}
表單.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>
結果.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
uj5u.com熱心網友回復:
問題出在我的模型中,我之前只是更改了欄位名稱,而忘記更改 getter 和 setter 名稱。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/407591.html
標籤:
上一篇:全部允許不授權匿名訪問
