org.springframework.validation.BindException----spring mvc 传递日期异常解决方法

来源:互联网 发布:优质三坐标编程 编辑:程序博客网 时间:2024/06/07 05:18

在Spring3 Mvc中从前台到后台传递数据中如果包括日期类型的话,一般会报错:

org.springframework.validation.BindException



解决方式:

1、 建立Java类DateConverter

import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.beans.propertyeditors.CustomDateEditor;import org.springframework.web.bind.WebDataBinder;import org.springframework.web.bind.support.WebBindingInitializer;import org.springframework.web.context.request.WebRequest;/** *  * spring3 mvc 的日期传递[前台-后台]bug: * org.springframework.validation.BindException * 的解决方式.包括xml的配置 * @author zhanglei * */public class DateConverter implements WebBindingInitializer {@Overridepublic void initBinder(WebDataBinder binder, WebRequest request) {SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//System.out.println("DateConverter implements WebBindingInitializer");binder.registerCustomEditor(Date.class, new CustomDateEditor(df,false));}}


2、 加入xml配置

<context:component-scan base-package="cn.com.swansoft.sms.web.controller" /><!-- 日期转换  必须放在<mvc:annotation-driven />前面 --><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><!-- 日期格式转换 --><property name="webBindingInitializer"><bean class="cn.com.swansoft.sms.web.service.DateConverter" /></property></bean><mvc:annotation-driven />



原创粉丝点击