需求,要求批量新增或者修改一个List,在springMVC中是不支持下面代码的写法:

@RequestMapping(value = "/update", method = RequestMethod.POST)
public String update(List<ProductCollocation> productCollocations ,HttpServletRequest request, RedirectAttributes redirectAttributes) {
  for (ProductCollocation productCollocation : productCollocations) {
    productCollocation.setModifyDate(DateUtil.getDate());
    productCollocationService.update(productCollocation, "create_date","product","collocation","description");
  }
  addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
  return "redirect:list.jhtml";
}

这样写会抛出如下异常:

nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: 

是否其实也很简单,Spring MVC 需要支持Form表单对象的方式映射,使用get set器来填充对象。

新增一个Form

public class ProductCollocationForm {
List<ProductCollocation> productCollocations; /**
* @return the productCollocations
*/
public List<ProductCollocation> getProductCollocations() {
return productCollocations;
} /**
* @param productCollocations the productCollocations to set
*/
public void setProductCollocations(List<ProductCollocation> productCollocations) {
this.productCollocations = productCollocations;
}
}

再使用Form来set对象

@RequestMapping(value = "/update", method = RequestMethod.POST)
public String update(ProductCollocationForm productCollocationForm ,HttpServletRequest request, RedirectAttributes redirectAttributes) {
    for (ProductCollocation productCollocation : productCollocationForm.getProductCollocations()) {
      productCollocation.setModifyDate(DateUtil.getDate());
      productCollocationService.update(productCollocation, "create_date","product","collocation","description");
    }
    addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
    return "redirect:list.jhtml";
}

前台就能够使用索引的方式对后台对象设置值了

<td>
  <input type="text" name="productCollocations[${productCollocation_index}].displayName" class="text" maxlength="200" style="width:100px" value="${productCollocation.displayName}"/>
  <input type="hidden" name="productCollocations[${productCollocation_index}].id" class="text" maxlength="200" value="${productCollocation.id}"/>
</td>

上面页面中name的值为:productCollocations[${productCollocation_index}].displayName,其实也相当于productCollocations[0].displayName、productCollocations[1].displayName类似这种的写法

SpingMVC实现集合参数(Could not instantiate bean class [java.util.List])的更多相关文章

  1. 【FAQ】SpingMVC实现集合參数(Could not instantiate bean class [java.util.List])

    需求,要求批量新增或者改动一个List,在Spring MVC中是不支持以下代码的写法 @RequestMapping(value = "/update", method = Re ...

  2. org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class

    错误:org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util ...

  3. Could not instantiate bean class [java.util.List]: Specified class is an interface] with root cause

    最近项目中页面比较复杂,springMVC传参过程中遇到这样一个错误:Could not instantiate bean class [java.util.List]: Specified clas ...

  4. spring mvc 如何传递集合参数(list,数组)

    spring mvc 可以自动的帮你封装参数成为对象,不用自己手动的通过request一个一个的获取参数,但是这样自动的参数封装碰碰到了集合参数可能就需要点小技巧才可以了. 一.基础类型和引用类型有什 ...

  5. Spring MVC在接收复杂集合参数

    Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是applica ...

  6. SpringMVC接收复杂集合参数

    Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是applica ...

  7. Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]: Specified class

    如果在使用SpringMVC中使用文件上传的MultipartFile对象时,出现了以下的错误: Could not instantiate bean class [org.springframewo ...

  8. springmvc上传文件报错org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]

    在用springmvc+mybatis进行项目开发时,上传文件抛异常... org.springframework.beans.BeanInstantiationException: Could no ...

  9. Spring4学习回顾之路04—引用其他Bean,集合数据注入,内部Bean

    引用其他Bean 组件应用程序的Bean经常需要相互协作以完成应用程序的功能,所以要求Bean能够相互访问,就必须在Bean配置文件中指定Bean的引用.在Bean的配置文件中可以用过<ref& ...

随机推荐

  1. ajax获取django的csrf_token

    ''' 方法一: data: { 'teamid': teamid, csrfmiddlewaretoken: '{{ csrf_token }}' //data: {name: 'john', cs ...

  2. shell脚本之正则表达和文本处理(文本处理三剑客:1、grep 2、sed 3、awk)

    文本处理三剑客:1.grep  2.sed  3.awk 一.grep:(过滤) grep的使用,主要的参数有: -n  :显示行号:-o  :只显示匹配的内容-q  :静默模式,没有任何输出,得用e ...

  3. heightCharts改变图表内图标的样式:链接外部图片

    series: [{ name: '东京', marker: { symbol: 'url(https://www.highcharts.com/demo/gfx/sun.png)'     //链接 ...

  4. 动态加载ajax 腾讯视频评论

    import urllib import urllib2 import os import requests import re import json sns_url = 'http://sns.v ...

  5. list 迭代器随机范围内移动

    Increments an iterator by a specified number of positions. template<class InputIterator, class Di ...

  6. AC日记——[NOIP2015]运输计划 cogs 2109

    [NOIP2015] 运输计划 思路: 树剖+二分: 代码: #include <cstdio> #include <cstring> #include <iostrea ...

  7. (转)Docker 基础 : Dockerfile

    全文来自 Docker 基础 : Dockerfile Dockerfile 是一个文本格式的配置文件,用户可以使用 Dockerfile 快速创建自定义的镜像.我们会先介绍 Dockerfile 的 ...

  8. 详解Oracle数据货场中三种优化:分区、维度和物化视图

    转 xiewmang 新浪博客 本文主要介绍了Oracle数据货场中的三种优化:对分区的优化.维度优化和物化视图的优化,并给出了详细的优化代码,希望对您有所帮助. 我们在做数据库的项目时,对数据货场的 ...

  9. 大数据技术之_16_Scala学习_07_数据结构(上)-集合

    第十章 数据结构(上)-集合10.1 数据结构特点10.1.1 Scala 集合基本介绍10.1.2 可变集合和不可变集合举例10.2 Scala 不可变集合继承层次一览图10.2.1 图10.2.2 ...

  10. Codeforces #442 Div2 F

    #442 Div2 F 题意 给出一些包含两种类型(a, b)问题的问题册,每本问题册有一些题目,每次查询某一区间,问有多少子区间中 a 问题的数量等于 b 问题的数量加 \(k\) . 分析 令包含 ...