需求,要求批量新增或者修改一个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. Cpython解释器支持的进程与线程

    一.理论部分 一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): egon在一个时间段内有很多任务要做:python备课的 ...

  2. 在oracle官网上,找到我们所需版本的jdk

    oracle的官网,因为都是英文,而且内容还特别多,经常的找不到历史版本的JDK. 特地,将找历史版本JDK的方法记录下来. 访问:http://www.oracle.com/technetwork/ ...

  3. vCard

    vCard 在翻阅dottoro的时候,在附录(appendix)的js部分,注意到一个叫vCard的部分,能单独列出来,可能是比较重要的,至少是比较独立的部分,但是以前从未听说或者了解过这一部分,如 ...

  4. js实现双指缩放图片 手机端双指缩放图片

    首先引入js文件,需要jq,pinchzoom.js.pinchzoom.js需要在jq环境下使用,可以 <meta name="viewport" content=&quo ...

  5. brew安装mysql

    1. 安装mysql5.7版本,不指定版本就默认安装最新的,目前最新是8 brew install mysql@5.7 2. 安装完进入/usr/local/Cellar/mysql@5.7目录,可以 ...

  6. 解决使用base64解码太慢的问题,原因是根本就不应该使用此方法解决。

    /* 功能:将中文内容的斜杠和双引号转了,方便保存到lua+ssdb中,从SSDB提取出来组装JSON时就不会出错. 作者:黄海 时间:2015-01-31 */ function jsonencod ...

  7. Web.Config文件配置之限制上传文件大小和时间

    在邮件发送系统或者其他一些传送文件的网站中,用户传送文件的大小是有限制的,因为这样不但可以节省服务器的空间,还可以提高传送文件的速度.下面介绍如何在Web.Config文件中配置限制上传文件大小与时间 ...

  8. UVA 11624 Fire!【两点BFS】

    Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...

  9. MongoDB走过的坑(4.0.3版本)

    数据存储一般使用本地或者存储在数据库,MongoDB是一个非关系型数据库,今天小结下走过的一些坑. 1.网上的很多教程对自己无效 解决方法:这种情况一般都是和版本有关系,数据库在不断的更新发展,很多东 ...

  10. [Hackerrank]时间转换Time Conversion

    题目链接 大致要求是说给定一个十二小时制的时间,给出它的二十四小时制的形式. 输入格式:hh:mm:ssAM 或者 hh:mm:ssPM,其中01≤hh≤12,00≤mm,ss≤59 思路 判断字符串 ...