解决spring4.x 版本的springmvc中提交数组时超过256个值报错问题

来源:互联网 发布:mac怎么搜索硬盘文件 编辑:程序博客网 时间:2024/04/30 08:22

  首先声明下我的环境,spring版本是4.3.2,框架是springmvc+mybatis,前端是easyui.

今天遇到个诡异的问题,就是前台在保存的时候,后台报错,提示类似如下信息:

org.springframework.beans.InvalidPropertyException : Invalid property 'list[256]' of bean class [com.AppController]: Index of out of bounds in property path 'list[256]'; nested exception is java.lang.IndexOutOfBoundsException: Index: 256, Size: 256 


之前也遇到过类似的问题,但是前台是可以保存的,只不过保存的内容被截取了,只保存了部分数据,当时处理的方式是修改tomcat的server.xml的配置,

原配置:

<Connector connectionTimeout="20000" port="7080" protocol="HTTP/1.1" redirectPort="8443" />

改后的配置:

<Connector connectionTimeout="20000" port="7080" protocol="HTTP/1.1" redirectPort="8443" 
    compression="on" executor="tomcatThreadPool" URIEncoding="utf-8"
    compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
    noCompressionUserAgents="gozilla, traviata" compressionMinSize="50" maxParameterCount="1000000"/>


当发现问题时,于是去查看tomcat的配置,结果发现已经添加了这些配置,但是仍旧不行。细想一下,两者是有区别的,改tomcat配置的情况,不是保存不了,是保存成功,但是保存不完全,而现在的问题是直接保存不了。

后来发现现在的程序中spring用的是4.3.2版本,而之前是3.0.5。然后去网上搜了很多资料,都说使用binder.setAutoGrowCollectionLimit(Integer.MAX_VALUE); 解决,但是setAutoGrowCollectionLimit这个方法貌似在spring4.x版本提供的,也就是说3.x版本没有这个方法,所以对于spring3.x版本,springmvc应该不会存在这个问题。

到了spring4.x这个值的默认值是256,所以需要重写这个方法,具体步骤如下:

1.添加如下类:

public class MyWebBindingInitializer implements WebBindingInitializer {    
    @Override  
    public void initBinder(WebDataBinder binder, WebRequest request) {   
    binder.setAutoGrowCollectionLimit(Integer.MAX_VALUE);
        binder.registerCustomEditor(Date.class, new DateConvertPropertyEditor());  
    }  
}  

2.在dispatch-servlet.xml中注入bean

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
     <property name="webBindingInitializer">  
      <bean class="com.xxx.MyWebBindingInitializer"/>  
     </property>

</bean>


就这么简单的搞定了。

另外需要注意一个坑,就是spring4.3.5版本之前,

binder.setAutoGrowCollectionLimit(Integer.MAX_VALUE);
binder.registerCustomEditor(Date.class, new DateConvertPropertyEditor()); 

两句的顺序不能颠倒,颠倒之后,setAutoGrowCollectionLimit不起作用。这个在spring4.3.5版本中修复了。

阅读全文
1 0
原创粉丝点击