Search code examples
springspring-mvccontrollerspring-4http-request-parameters

Spring's @RequestParam with Nested Objects / Rich Model Objects


Simple problem:

@Controller
class MyController {
  @RequestMapping(...)
  void test(MyModel m) {
    ...
  }
}

class MyModel {
  MyNestedModel a;
}

class MyNestedModel {
  @RequestParam("b[]")
  List<String> b;
}

This apperantly does not work, because @RequestParam only works with method parameters.

Is there a way to define the name of the request param within the model object?


Reason:

My MyModel and MyNestedModel classes is of course much bigger and I'd like to use for example ?a.b[]=TEST.


Thanks for your help :)


EDIT: Looks like this is exactly my problem: How to customize parameter names when binding spring mvc command objects


Solution

  • Spring mvc can transfer the parameter for you. But the post data should be like:

    {a.b[0] : "b1", a.b[1] : "b2"}
    

    then you can get a list in m.a.b