SpingMVC实现集合参数(Could not instantiate bean class [java.util.List])
2024-08-25 09:13:09
需求,要求批量新增或者修改一个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类似这种的写法
最新文章
- 移动端事件touchstart、touchmove、touchend
- ES6箭头函数
- java web上传下载乱码问题解决方法
- Web前端开发基础 第四课(盒代码模型)
- CAS无锁算法与ConcurrentLinkedQueue
- 用dbforge调试procedure
- Android 使用finalBitmap实现缓存读取
- itextsharp、Aspose.Words、Aspose.Cells联合使用
- *[topcoder]ChooseTheBestOne
- Yslow 之 高性能网站建设指南(High.Performance.Web.Sites)
- CPP变量参数别名
- Android 使用BroadcastReceiver的几种方法
- 使用 NuGet 管理项目库
- HDU 5543 Pick The Sticks
- PTA_Have fun with numbers(C++)
- css样式之补充。。。
- flex布局应用与踩坑
- 2018.4.24 快排查找第K大
- 李清华201772020113《面向对象程序设计(Java)》第六周学习总结
- 机器学习技法笔记:08 Adaptive Boosting
热门文章
- selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
- wx.ScrolledWindow wx.PseudoDC
- pandas 读写sql数据库
- Asp.Net Core 项目实战参考
- Delphi Socket通信及多线程编程总结
- POJ 1321 棋盘问题 (DFS + 回溯)
- 树链剖分【CF343D】Water Tree
- 洛谷——P1480 A/B Problem
- java应用高内存占用
- 12、Django实战第12天:课程机构列表页数据展示