【问题标题】:Spring's @RequestParam with Nested Objects / Rich Model ObjectsSpring 的 @RequestParam 与嵌套对象/富模型对象
【发布时间】:2014-09-16 01:02:12
【问题描述】:

简单的问题:

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

class MyModel {
  MyNestedModel a;
}

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

这显然不起作用,因为@RequestParam 仅适用于方法参数。

有没有办法在模型对象中定义请求参数的名称?


原因:

我的MyModelMyNestedModel 课程当然要大得多,例如我想使用?a.b[]=TEST


感谢您的帮助:)


编辑:看起来这正是我的问题:How to customize parameter names when binding spring mvc command objects

【问题讨论】:

标签: spring spring-mvc controller spring-4 http-request-parameters


【解决方案1】:

Spring mvc 可以为你传递参数。但是帖子数据应该是这样的:

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

那么你可以在m.a.b中得到一个列表

【讨论】:

  • 没有POST,没有JSON。我需要数据作为GET 参数:http;//localhost/test?a.b[]=TEST
  • 你试试localhost/test?a.b[0]=TEST。我认为 spring mvc 将以相同的方式处理参数。而且我知道帖子运行良好。
  • 奇怪,这行得通。现在我有List&lt;String&gt; b 并尝试了以下请求:?b=x&amp;b=y (works: [x, y]), ?b=x,y (works: [x, y]), ?b[]=x (fails), ?b[0]=x (works : [x]), ?b[1]=y (作品: [null, y])。而当使用Set&lt;String&gt; b 时,我只能使用?b=x&amp;b=y?b=x,y?b[0]=x 不适用于 Set。似乎没有简单的方法来处理?b[]=x
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-16
  • 1970-01-01
  • 2014-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多