How to pass thymeleaf(th:object) values to controller.
HTML:
<form id="searchPersonForm" action="#" th:object="${person}" method="post" >
</form>
SearchPersonController:
@RequestMapping(value = "/modify/{pid}", method = RequestMethod.GET)
public String modifyPersonData(Principal principal, @ModelAttribute("person") Person person, UserRoles userRoles, Model model, @PathVariable("pid") Long pid ) {
//modify data
}
I am trying to pass like @ModelAttribute("person") Person person
but this is not retrieving form values from previous page.
Can anyone help on this.
Thanks.
Preferably use
th:action
as a form attribute instead ofaction
, and specify the binding like the following:I back this form with a Spring controller that initializes the model attribute (
myEntity
object in the form). This is the relevant part of the controller class:The
@ModelAttribute
annotation ensures that the model object is initialized by Spring for every request.Set a model named "command" during the first get request to your controller:
And, to access the model as it results after the form submission, implement the relative method:
Here, the parameter annotated with
@ModelAttribute
is automatically bound to the submitted object.