SpringMVC前台提交参数绑定list时大小超过256 java.lang.IndexOutOfBoundsException解决办法

来源:互联网 发布:淘宝宝贝推荐代码生成 编辑:程序博客网 时间:2024/04/30 13:59

springMVC框架提交参数list时,springmvc默认只能接收到255个数据,当你前台传的数据长度大于255位的时候就会报错
例如我在前台传输的solutionModelList的长度有364位,在chrome控制台可以看到
这里写图片描述
然后在后台接收

 @RequestMapping("/saveFinance.json") @ResponseBody  public AjaxObj saveFinance(List<SolutionModelEntity> solutionModelList) {}

报错
这里写图片描述

解决办法:在你的controller层加上初始化方法

//这个类初始化的时候会首先调用init方法   @InitBinder  //类初始化是调用的方法注解    public void initBinder(WebDataBinder binder) {          //给这个controller配置接收list的长度100000,仅在这个controller有效        binder.setAutoGrowCollectionLimit(100000);     }

但是这个方法之针对这样处理过的类有用,要想在整个项目下都定义长度,如下:

定义一个初始化类public class myInitializer implements WebBindingInitializer {      @Override      public void initBinder(WebDataBinder binder) {          binder.setAutoGrowCollectionLimit(100000);      }  }然后在配置文件配置<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">      <property name="webBindingInitializer">          <bean class="xxx.myInitializer"/>  //classmyInitializer类    </property>  </bean>  
1 0
原创粉丝点击