首页 > 代码库 > 【FAQ】SpingMVC实现集合參数(Could not instantiate bean class [java.util.List])

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

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

	@RequestMapping(value = http://www.mamicode.com/"/update", method = RequestMethod.POST)"font-family: Arial, Helvetica, sans-serif;">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 = http://www.mamicode.com/"/update", method = RequestMethod.POST)>
前台就能够使用索引的方式对后台对象设置值了


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



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