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

我正在尝试创建一个HTML网络表单,该表单可以创建Sponsored Events(一个Model类),我有一个Spring Controller,一个Thymeleaf视图以及Entity模型。但是,无论尝试什么,我似乎都无法工作。

我正在使用此页面作为参考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
@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时出错(模板:“事件/创建”-第9行,第26行)

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

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

本文链接:https://www.f2er.com/3169496.html

大家都在问