gpt4 book ai didi

java - 为什么是:field not working in Spring View

转载 作者:行者123 更新时间:2023-12-02 09:21:11 25 4
gpt4 key购买 nike

我正在尝试创建一个 HTML Web 表单来创建赞助 Activity (一个模型类),我有一个 Spring Controller 、一个 Thymeleaf View 和实体模型。但是,无论我如何尝试,我似乎都无法让 th:field 正常工作。

我正在使用此页面作为引用https://spring.io/guides/gs/handling-form-submission/

查看

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<title>Create a Sponsored Event</title>
</head>
<body>
<h1>Create a Sponsored Event</h1>
<form action="#" th:action="@{/event/create/submit}" th:object="${sponsor}" method="post">
<input type="text" th:field="*{id}"/> <!-- This Line -->
</form>
</body>
</html>

Controller

@Controller
@RequestMapping("events")
public class SponsorController {

private static final String CREATE_PAGE = "events/create";

@GetMapping("/create")
public String addSponsorEvent(Model model) {
model.addAttribute("sponsor", new Sponsor());

return CREATE_PAGE;
}
}

型号

@Data
@Entity
@NoArgsConstructor
@Accessors(fluent = true)
@Table(name = "sponsor_form")
public class Sponsor {

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

}

我也尝试过改变这个

<input type="text" th:field="*{id}"/>
<input type="text" th:field="*{id()}"/>
<input type="text" th:field="*{sponsor.id}"/>
<input type="text" th:field="*{sponsor.id()}"/>

我收到错误:

Caused by: org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "events/create" - line 9, col 26)

最佳答案

在 Model 类 Sponsors 中,错误是由 @Accessors( Fluent = true) 引起的,我已删除此行并解决了问题。

关于java - 为什么是:field not working in Spring View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58686190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com