【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 = "/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>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118343.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java list高效去重_简述Java List去重五种方法「建议收藏」

    java list高效去重_简述Java List去重五种方法「建议收藏」搜索热词前言去重,对于很多场合必不可少。写此篇文章是因为在之前做某个画面中,我在数据库中进行Distinct和OrderBy去重,发现影响效率,故此在后台先做去重处理;所以记录此文,已做参考:Num1:使用java8新特性stream进行List去重publicstaticvoidmain(String[]args){Listlist=newArrayList();li…

    2022年5月25日
    48
  • 数据仓库(四)之ETL开发

    数据仓库(四)之ETL开发 概述 ETL是数据仓库的后台,主要包含抽取、清洗、规范化、提交四个步骤,传统数据仓库一般分为四层模型。               分层的作用                                      STG层  在维度建模阶段已经确定了源系统,而且对源系统进行了…

    2022年6月13日
    32
  • Windows 10上如何安装ubuntu虚拟机「建议收藏」

    因为开发的需要,很多时候时候我们需要安装虚拟机,然后在虚拟机上面安装我们需要的操作系统。最近也是因为学习的需要,就用自己的Windows10安装了ubuntu虚拟机,在虚拟机上面安装需要的操作系统,下面是具体的操作过程。一下载准备文件(1)先去下载VirtualBox,这里我用的版本是VirtualBox-6.1.4-136177-Win.exe,官网地址:https://www.vi…

    2022年4月17日
    354
  • InetAddress类[通俗易懂]

    InetAddress类[通俗易懂]基本概念1.InetAddress位于java.net包下2.InetAddress用来代表一个IP地址,一个InetAddress对象就代表着一个IP地址3.实例化InetAddress的方法是:调用InetAddress.getByName(Stringhost);4.获取IP对应的域名:用InetAddress的实例化对象调用getHostName();5.获取IP的地址:用In…

    2022年6月23日
    20
  • Oracle 判断 并 手动收集 统计信息 脚本

    Oracle 判断 并 手动收集 统计信息 脚本

    2021年9月17日
    43
  • git删除本地分支和远程分支_git删除远程分支

    git删除本地分支和远程分支_git删除远程分支git上面的分支开发完成以后,完成了他的历史使命,就可以删除了。1.删除本地分支查看本地分支gitbranchadd_jvm_config_and_exception_loghdfs_config_in_zk*mastersubBucket删除已经merge的本地分支gitbranch-dadd_jvm_config_and_exception_log…

    2022年10月9日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号