【FAQ】SpingMVC实现集合參数(Could not instantiate bean class [java.util.List])
需求,要求批量新增或者改动一个List,在Spring MVC中是不支持以下代码的写法
@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]:
是否事实上也非常easy,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>
【FAQ】SpingMVC实现集合參数(Could not instantiate bean class [java.util.List])的更多相关文章
- SpingMVC实现集合参数(Could not instantiate bean class [java.util.List])
需求,要求批量新增或者修改一个List,在springMVC中是不支持下面代码的写法: @RequestMapping(value = "/update", method = Re ...
- 集合框架的类和接口均在java.util包中。 任何对象加入集合类后,自动转变为Object类型,所以在取出的时候,需要进行强制类型转换。
集合框架的类和接口均在java.util包中. 任何对象加入集合类后,自动转变为Object类型,所以在取出的时候,需要进行强制类型转换.
- springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定
springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定 标签: springmvc springmvc学习笔记13-springmvc注解开发之集合类型參数绑定 数组绑定 需 ...
- EM算法求高斯混合模型參数预计——Python实现
EM算法一般表述: 当有部分数据缺失或者无法观察到时,EM算法提供了一个高效的迭代程序用来计算这些数据的最大似然预计.在每一步迭代分为两个步骤:期望(Expectation)步骤和最大化( ...
- grep命令经常使用參数及使用方法
1.grep介绍 grep命令是Linux系统中一种强大的文本搜索工具,它能使用正則表達式搜索文本.并把匹 配的行打印出来.grep全称Global Regular Expression Print, ...
- 增强for循环、Map接口遍历、可变參数方法
增强for循环 1.for循环能做得事情.增强for循环大部分都能做(假设要想获得下标的时候就必须使用简单for循环了) 2.增强for有时候可以方便的处理集合遍历的问题,可是集合的标准遍历是使用迭代 ...
- matplotlib画图实例:pyplot、pylab模块及作图參数
http://blog.csdn.net/pipisorry/article/details/40005163 Matplotlib.pyplot画图实例 {使用pyplot模块} matplotli ...
- pthread_create()创建线程时传入多个參数
因为接口仅仅定义了一个入參void *arg int pthread_create(pthread_t *tidp,const pthread_attr_t *attr, (void*)(*start ...
- 关于mybatis中,批量增删改查以及參数传递的问题
1.參数传递的问题 大多数情况下,我们都是利用map作为參数,而且大部分情况下都是仅仅有一个參数. 可是,我们也能够利用@param注解,来传入多个參数,此时,mybatis会自己主动将參数封装成ma ...
随机推荐
- java 利用java运行时的方法得到当前屏幕截图的方法(转)
将截屏图片保存到本地路径: package com.test; import java.awt.AWTException; import java.awt.Dimension; import java ...
- cocos2dx进阶学习之场景切换
背景 在学习马里奥时,我们学习到从菜单场景到游戏场景的切换,代码如下 void CMMenuScene::OnStartCallBack( CCObject *pSender ) { CCDirect ...
- Dalvik虚拟机的优化机制
Dalvik虚拟机设计作为Android系统定制虚拟机, 在移动设备上运行,必须要比普通的Java虚拟机有更多的优化手段和机制, 以下就列举出其中主要的一些优化机制: 1. 使用dex格式的类文件,可 ...
- haml、sass简单的解释
1. Haml 全名为 HTML Abstract Markup Language,主要就是让开发者能够使用缩排的方式撰写 HTML,做到永不忘记关 Tag 的效果. 例如:%h1= "He ...
- JS+CSS打造三级折叠菜单,自动收缩其它级 js
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...
- [置顶] Linux下文件和目录权限说明
在Linux下使用ls -l或者ll命令可以查看文件和文件夹的权限.结果显示类似于: drwxrwxrwx,这里分为四组,分别为文件类型,文件所有者的权限(读写执行),文件所有者所在组用户的权限(读写 ...
- win7下wordPress本地搭建博客详解(深度亲测整理---傻瓜式详细教程)
搭建一个wordPress作为一个个人博客本来是特别简单的事情,但是网上的各种转载让初学者举步维艰,我就本身条件而言,会java EE,懂mysql都花费了我好长时间才搭建好本地博客. 注意:这个是本 ...
- 创建RDD的方式
创建RDD的方法: JavaRDD<String> lines = sc.textFile("hdfs://spark1:9000/spark.txt"); Jav ...
- arm:jlink调试和直接烧写运行的不同 [mdk s3c2440]
1.对全局变量的初始化. 2.还没发现的事例. /*************************************************/ 先上连接文件sct LR_ROM1 0x3000 ...
- Java进阶05 多线程
链接地址:http://www.cnblogs.com/vamei/archive/2013/04/15/3000898.html 作者:Vamei 出处:http://www.cnblogs.com ...