在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 {
@Override
public 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 />前面 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<!-- 日期格式转换 -->
<property name="webBindingInitializer">
<bean class="cn.com.swansoft.sms.web.service.DateConverter" />
</property>
</bean>
<mvc:annotation-driven />