我是Thymeleaf的新手,我尝试使用Thymeleaf和Spring MVC执行一个简单的表单提交示例。我根据Thymeleaf文档编写了代码。但我在控制器中得到空值。
<form action="thymeleafexample/thymeleaf.html" th:action="@{/thymeleaf}"
th:object="${loginModel}" method="post">
<table>
<tr>
<td th:text="#{emp.empId.label}">Emp ID</td>
<td><input type="text" th:field="*{empId}"/></td>
</tr>
<tr>
<td th:text="#{emp.empName.label}">Emp Name</td>
<td><input type="text" th:field="*{empName}"/></td>
</tr>
<tr>
<td>
<button type="submit" name="save" th:text="#{${'.emp.button.label'}}">submit</button>
</td>
</tr>
</table>
</form>
我的控制器是
@RequestMapping(value = "/thymeleaf", params = {"save"})
public String save(@Validated LoginModel loginModel, final BindingResult bindingResult, ModelMap model) {
if (bindingResult.hasErrors()) {
return "thymeleaf";
}
System.out.println(loginModel);
System.out.println(loginModel.getEmpName());
return "/thymeleaf";
}
我的Model类是
public class LoginModel {
private String empName;
private int empId;
public void setEmpId(int empId) {
this.empId = empId;
}
public int getEmpId() {
return empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}
答案 0 :(得分:0)
我遇到了同样的问题和as OP mentioned,为POJO(Model)类创建了一个构造函数,其中包含必要的成员字段,并使用th:field=*{foo}
代替th:value=${foo}
解决了我的问题。