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

我正在尝试创建一个 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栏)


白衣染霜花
浏览 36回答 1
1回答

开满天机

在 Model 类 Sponsors 中,错误是由 @Accessors( Fluent = true) 引起的,我删除了这一行并解决了问题。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java