How to pass th:object values from html to controller
Asked Answered
C

1

13

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.

Corrianne answered 17/8, 2015 at 7:54 Comment(0)
R
17

Preferably use th:action as a form attribute instead of action, and specify the binding like the following:

<form th:action="@{/the-action-url}" method="post"
    th:object="${myEntity}">

    <div class="modal-body">
        <div class="form-group">
            <label for="name">Name</label> <input type="text"
                class="form-control" id="name" th:field="*{name}"> </input>
        </div>

        <div class="form-group">
            <label for="description">Description</label> <input type="text"
                class="form-control" id="description"
                th:field="*{description}"> </input>
        </div>
    </div>
</form>

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:

@ModelAttribute(value = "myEntity")
public Entity newEntity()
{
    return new Entity();
}

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:

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView getRanks(Model model, HttpServletRequest request)
{
    String view = "the-view-name";
    return new ModelAndView(view, "command", model);
}

And, to access the model as it results after the form submission, implement the relative method:

@RequestMapping(value = "/the-action-url", method = RequestMethod.POST)
public View action(Model model, @ModelAttribute("myEntity") Entity myEntity)
{
    // save the entity or do whatever you need

    return new RedirectView("/user/ranks");
}

Here, the parameter annotated with @ModelAttribute is automatically bound to the submitted object.

Roundy answered 17/8, 2015 at 15:28 Comment(3)
Thankyou for the explanation. Can I ask few doubts. 1. I have currents 5 submit buttons in the current page(search page) like search, copy, view /modify ,.. So we don't have a default URL to specify for th:action="@{/the-action-url}" . 2.Earlier I was using ` RequestMethod.POST` for modifyPersonData and I got Person form values from view by just using Person person without @ModelAttribute("person") . But when I changed to ` RequestMethod.GET, I am not able to retrieve Person ` values and I tried with @ModelAttribute("person") Person person and this is not working too.Corrianne
1. You can use 5 forms (one for each button) with different action urls OR an additional parameter (e.g. a hidden field) and a bit of javascript to discriminate the button that's been pressed. 2.If you map the controller method to GET requests ( @RequestMapping(value = "/modify/{pid}", method = RequestMethod.GET) ), then it won't match with the form, that is set to use post.Roundy
Thankyou EvilTodd. I want to map the controller to GET requests, so have to change form as get. But this is passing all input fields in the url. So I thought modelattribute can handle when the form is set to post and controller is get.Corrianne

© 2022 - 2024 — McMap. All rights reserved.