gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:01:23 25 4
gpt4 key购买 nike

我想通过 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不支持这种绑定(bind),所以你必须使用@ModelAttribute:

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

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

关于java - 对象列表作为 Spring MVC 中的 RequestParam,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6269042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com