webBindingInitializer 在XML中无效,spring mvc 3.2以版本

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mmm333zzz/article/details/56015387

    这个仁兄说得好……https://www.oschina.net/question/140550_20454

    default message [Failed to convert property value of type 'Java.lang.String' to required type 'java.util.Date' for property 'sdate';

package com.lanyuan.util;
import java.text.SimpleDateFormat;   
import java.util.Date;   
import org.springframework.core.convert.converter.Converter;
  
/** 
 * spring4 mvc 的日期传递[前台-后台]bug:  
 * 的解决方式.包括xml的配置  
 *  new SimpleDateFormat("yyyy-MM-dd"); 或 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
 *    这里的日期格式必须与提交的日期格式一致 
 * @author lanyuan 
 * Email:[email protected] 
 * date:2017-2-20 
 */  
public class SpringMVCDateConverter implements Converter<String, Date> {  
	@Override  
	public Date convert(String source) {  
	    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    dateFormat.setLenient(false);  
	    try {  
	        return dateFormat.parse(source);  
	    } catch (Exception e) {  
	    	dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
		    dateFormat.setLenient(false);  
		    try {  
		        return dateFormat.parse(source);  
		    } catch (Exception e1) {  
		    	e1.printStackTrace();
		    }
	    }         
	    return null;  
	}
}


在spring mvc 的配置文件 spring-mvc.xml 添加或修改如下
<mvc:annotation-driven conversion-service="conversionService" /> 






猜你喜欢

转载自blog.csdn.net/mmm333zzz/article/details/56015387