Spring MVC中作为RequestParam的对象列表 [英] List of Objects as RequestParam in Spring MVC

查看:158
本文介绍了Spring MVC中作为RequestParam的对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 POST 向对象发送一个对象ID列表(由用户选择复选框生成),这样我就可以得到一个 java.util.List< MyObject> 使用 MyObjectEditor 转换。

I want to send a list of object id's (generated by the user selecting checkboxes) by POST to an action, so I can get a java.util.List<MyObject> converted using an MyObjectEditor.

那么,是否可以这样做?

So, is it possible to do this?

@InitBinder
public void initBinder (WebDataBinder binder) {
    binder.registerCustomEditor(MyObject.class, new MyObjectEditor());
}
@RequestMapping (value = "", method = RequestMethod.POST)
public String action (@RequestParam List<MyObject> myList, Model model) {
    // more stuff here
}

我的POST就像这样:

And my POST would be like this:

myList[0] = 12
myList[1] = 15
myList[2] = 7

谢谢!

推荐答案

@RequestParam 不支持这种绑定,因此您必须使用 @ModelAttribute

This kind of binding is not supported by @RequestParam, so you have to use @ModelAttribute:

class MyObjects {
    private List<MyObject> myList;
    ...
}

public String action (@ModelAttribute MyObjects myObjects, Model model) { ... }

这篇关于Spring MVC中作为RequestParam的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆