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

标签 java spring spring-mvc

我想通过 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/

相关文章:

java - 在没有互联网连接到 Wifi 时使用蜂窝数据

java - 使用流、无映射、无键值对计算和打印文本中的字符频率

java - Java中的条件评估过程

java - Spring @Transactional 属性是否适用于私有(private)方法?

java - Spring MVC 2.5 servlet/portlet 兼容 URL

java - 强制 MapStruct 使用自定义方法而不是 dto 值

java - Spring 无法 Autowiring 字段,没有匹配的类型的 bean

java - Spring AbstractAnnotationConfigDispatcherServletInitializer 未找到 WebApplicationContext : no ContextLoaderListener registered

java - 在执行异步处理时持有 http 请求

java - 这个 Spring Security 图意味着什么?