Note that there are some explanatory texts on larger screens.

plurals
  1. POData binding of @ModelAttribute annotated parameters
    primarykey
    data
    text
    <p>I have a Spring 2.5 annotated Controller in which I have a method annotated with @RequestMapping(method=RequestMethod.GET), which performs some logic to fill the model.</p> <p>I also have a method annotated with @RequestMapping(method=RequestMethod.POST) which performs the request. This method has a @ModelAttribute annotated parameter that contains my own form pojo, let's call it MyForm. I also have an initialization method for MyForm, also annotated with @ModelAttrribute. Now so far all works as expected: on a POST request the form data binds to MyForm and I can process it.</p> <p>The problem is that I want to be able to pre-populate the form by passing in (GET) request parameters. Since I have the @ModelAttribute method for MyForm, I do get a MyForm instance in my model, but it doesn't get populated unless I specifically use it as a parameter for my GET method.</p> <p>Why do I have to do this, is it possible to force data binding on my form for a GET request in a different way? I now just pass in the parameter, but because it is already in the model I don't have to do anything with it, resulting in an ugly unused method parameter.</p> <p>[Edit: some code examples to illustrate]</p> <p>The controller that doesn't populate the form on a get request:</p> <pre><code>@Controller public class MyController { @ModelAttribute("myForm") public MyForm createForm() { return new MyForm(); } @RequestMapping(method=RequestMethod.GET) public void handlePage(Model model) { //Do some stuff to populate the model.... } @RequestMapping(method=RequestMethod.POST) public void processForm(@ModelAttribute("myForm") MyForm myForm) { //Process the form } } </code></pre> <p>When I change the method signature of the handlePage method, it gets populated on a get request...</p> <pre><code>@RequestMapping(method=RequestMethod.GET) public void handlePage(Model model, @ModelAttribute("myForm") MyForm myForm) { //Do some stuff to populate the model.... } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload