[英]Unable to get thymeleaf form data in spring mvc
I am new to Thymeleaf, I try to execute a simple form submittion example using Thymeleaf and Spring MVC. 我是Thymeleaf的新手,我尝试使用Thymeleaf和Spring MVC执行一个简单的表单提交示例。 I wrote the code according to Thymeleaf documentation. 我根据Thymeleaf文档编写了代码。 But I am getting null values in the controller. 但是我在控制器中得到了空值。
<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>
and my Controller is 而我的控制器是
@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";
}
and my Model class is 我的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;
}
}
我遇到了同样的问题, 正如OP所述 ,使用必要的成员字段为POJO(Model)类创建构造函数,并使用th:field=*{foo}
而不是th:value=${foo}
解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.