【FAQ】SpingMVC实现集合參数(Could not instantiate bean class [java.util.List])
发布日期:2021-06-24 18:06:43 浏览次数:4 分类:技术文章

本文共 1781 字,大约阅读时间需要 5 分钟。

需求,要求批量新增或者改动一个List,在Spring MVC中是不支持以下代码的写法

@RequestMapping(value = "/update", method = RequestMethod.POST)	public String update(List
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
productCollocations; /** * @return the productCollocations */ public List
getProductCollocations() { return productCollocations; } /** * @param productCollocations the productCollocations to set */ public void setProductCollocations(List
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";	}
前台就能够使用索引的方式对后台对象设置值了

				   				   				

转载地址:https://blog.csdn.net/weixin_34248258/article/details/85661019 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:2014第42周日当无聊时做什么
下一篇:共享锁与排它锁

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月07日 02时04分38秒