【发布时间】:2014-07-10 07:55:48
【问题描述】:
我是 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";
}
我的模型类是
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;
}
}
【问题讨论】:
标签: java spring-mvc thymeleaf