需求,要求批量新增或者修改一个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类似这种的写法

最新文章

  1. 移动端事件touchstart、touchmove、touchend
  2. ES6箭头函数
  3. java web上传下载乱码问题解决方法
  4. Web前端开发基础 第四课(盒代码模型)
  5. CAS无锁算法与ConcurrentLinkedQueue
  6. 用dbforge调试procedure
  7. Android 使用finalBitmap实现缓存读取
  8. itextsharp、Aspose.Words、Aspose.Cells联合使用
  9. *[topcoder]ChooseTheBestOne
  10. Yslow 之 高性能网站建设指南(High.Performance.Web.Sites)
  11. CPP变量参数别名
  12. Android 使用BroadcastReceiver的几种方法
  13. 使用 NuGet 管理项目库
  14. HDU 5543 Pick The Sticks
  15. PTA_Have fun with numbers(C++)
  16. css样式之补充。。。
  17. flex布局应用与踩坑
  18. 2018.4.24 快排查找第K大
  19. 李清华201772020113《面向对象程序设计(Java)》第六周学习总结
  20. 机器学习技法笔记:08 Adaptive Boosting

热门文章

  1. selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
  2. wx.ScrolledWindow wx.PseudoDC
  3. pandas 读写sql数据库
  4. Asp.Net Core 项目实战参考
  5. Delphi Socket通信及多线程编程总结
  6. POJ 1321 棋盘问题 (DFS + 回溯)
  7. 树链剖分【CF343D】Water Tree
  8. 洛谷——P1480 A/B Problem
  9. java应用高内存占用
  10. 12、Django实战第12天:课程机构列表页数据展示