Search code examples
springmodelattribute

Spring3 ModelAttribute from jsp request params


Is it possible to grab various form components and turn them into their corresponding DAO objects? I'm trying to avoid creating a custom Bean (which encapsulates all fields) for each Form.

Ex:

<form action="/add">
  <input name="foo" id="foo" value="29"/>
  <input name="bar" id="bar" value="63"/>
</form>


public void add(@ModelAttribute("foo") Foo myFoo, @ModelAttribute("bar") Bar myBar)

Currently, I'd have to use the HttpRequest, extract the values (29,63), and use them as primary keys to look up the correct database objects. I didn't know if there was a quick way to auto-wire this using ModelAttribute.

Thanks!


Solution

  • You should use @RequestParam

    public void add(@RequestParam("foo") Foo myFoo, @RequestParam("bar") Bar myBar) {
    }