Spring mvc, thymeleaf and forms data binding

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Spring mvc, thymeleaf and forms data binding

I'm starting with spring mvc and thymeleaf, I'm coming from struts2 and Im finding some dificulties working with forms. For example I have the following controller:

@RequestMapping(value = {"backend/customer/create"}, method = RequestMethod.GET)
public String showForm(Model model) {
  Customer customer = new Customer();
  customer.setFirstName("sample name");
  model.addAttribute("customer", customer);
  return "backend/customer/test";

@RequestMapping(value = {"backend/customer/create"}, method = RequestMethod.POST)
public String processForm(Customer customer, String firstName) {
  return "redirect:/backend/customer";

and the following form:

<form method="post" th:action="@{/backend/customer/create}">
  <input type="text" name="customer.firstName" th:value="${customer.firstName}"/>
  <input type="text" name="firstName" value="another name"/>
  <input type="submit" value="send">

The view output is ok, it shows "sample name" in the first input and "another name" in the second input.
But when I send the form I get in the controller customer.getFirstName()="another name" and firstName="another name"

I don't know why is it binding customer.firstName to firstName input if I'm not using th:object on my form. Can anyone tell me what am I doing wrong?