HomeController.java
@Controller
public class HomeController {
@RequestMapping("/")
public String home(Model model) {
model.addAttribute("formData", new User());
return "index";
}
}
index.html
<body>
<p>Enter data below</p>
<form action="/create" method="post" th:object="${formData}">
<p>
Full Name: <input type="text" th:field="${formData.fullName}" />
</p>
<p>
Age: <input type="text" th:field="${formData.age}" />
</p>
<p>
Employed: <input type="checkbox" th:field="${formData.employed}"
th:value="true" />
</p>
<p>Choose Gender :</p>
<p>
Male <input type="radio" th:field="${formData.gender}"
th:value="Male" /> Female <input type="radio"
th:field="${formData.gender}" th:value="Female" />
</p>
<p>
<input type="submit" value="Submit" /> <input type="reset"
value="Reset" />
</p>
</form>
</body>
User.java
public class User {
private String fullName;
private int age;
private boolean employed;
private String gender;
public User() {}
public User(String fullName, int age, boolean employed, String gender) {
super();
this.fullName = fullName;
this.age = age;
this.employed = employed;
this.gender = gender;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isEmployed() {
return employed;
}
public void setEmployed(boolean employed) {
this.employed = employed;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
这就是三个组件。我试图通过thymeleaf来绑定这个html。但是每次我都会得到错误的提示:
An error happened during template parsing (template: “class path resource [templates/index.html]”)
所有的getters和setters都是从源代码中生成的。如何将用户对象绑定到index html文件上?
StackOverflow:java - An error happened during template parsing (spring boot thymeleaf integration with html file) - Stack Overflow