为了账号安全,请及时绑定邮箱和手机立即绑定

为什么 th:field 在 Spring View 中不起作用

为什么 th:field 在 Spring View 中不起作用

白衣染霜花 2024-01-28 16:13:48
我正在尝试创建一个 HTML Web 表单来创建赞助活动(一个模型类),我有一个 Spring 控制器、一个 Thymeleaf 视图和实体模型。然而,无论我如何尝试,我似乎都无法让 th:field 正常工作。我使用此页面作为参考https://spring.io/guides/gs/handling-form-submission/看法<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org">  <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@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()}"/>我收到错误:引起原因:org.attoparser.ParseException:执行处理器“org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor”期间出错(模板:“events/create”-第9行,第26栏)
查看完整描述

1 回答

?
开满天机

TA贡献1786条经验 获得超12个赞

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



查看完整回答
反对 回复 2024-01-28
  • 1 回答
  • 0 关注
  • 31 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信