2

I am new to Thymeleaf, I try to execute a simple form submittion example using Thymeleaf and Spring MVC. I wrote the code according to Thymeleaf documentation. 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

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; } } 
8
  • <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> <tr> <td> <button type="submit" name="save" th:text="#{${'.emp.button.label'}}">submit</button> Commented Jul 10, 2014 at 7:57
  • Is your controller method actually getting invoked correctly? Commented Jul 10, 2014 at 8:33
  • thaks for your reply, yeah its invoking correctly. Commented Jul 10, 2014 at 10:27
  • Try removing params={"save"} and let me know Commented Jul 10, 2014 at 10:33
  • i tried but same result is coming Commented Jul 10, 2014 at 11:44

1 Answer 1

1

I was having the same problem and as OP mentioned, creating a constructor for the POJO(Model) class with necessary member fields and using th:field=*{foo} instead of th:value=${foo} solved my issue.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.