加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java-Spring MVC中作为RequestParam的对象列表

发布时间:2020-12-15 01:19:36 所属栏目:大数据 来源:网络整理
导读:我想通过POST向操作发送对象ID列表(由用户选择复选框生成),因此可以获取java.util.List .使用MyObjectEditor进行转换. 那么,有可能这样做吗? @InitBinderpublic void initBinder (WebDataBinder binder) { binder.registerCustomEditor(MyObject.class,new

我想通过POST向操作发送对象ID列表(由用户选择复选框生成),因此可以获取java.util.List< MyObject>.使用MyObjectEditor进行转换.

那么,有可能这样做吗?

@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将是这样的:

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

谢谢!

最佳答案
@RequestParam不支持这种绑定,因此您必须使用@ModelAttribute:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读