前台时间(如2013-08-12 18:10:23)传到后台srpingMVC 进行绑定到javaBean的util.date 时会报数据绑定失败,不能从String 转换到Date 类型。

这里我们无非就是想办法进行数据类型转换和绑定。具体绑定参见:http://aokunsang.iteye.com/blog/1409505  这位博主总结得很到位。

现在我写了一个自定议数据绑定类

[java] view plaincopyprint?

  1. package com.ltkj.zhepg.com.util;  

  2.   

  3. import java.text.SimpleDateFormat;  

  4. import java.util.Date;  

  5.   

  6. import org.springframework.beans.propertyeditors.CustomDateEditor;  

  7. import org.springframework.web.bind.WebDataBinder;  

  8. import org.springframework.web.bind.support.WebBindingInitializer;  

  9. import org.springframework.web.context.request.WebRequest;  

  10.   

  11. /** 

  12.  * pring3 mvc 的日期传递[前台-后台]bug:  

  13.  * org.springframework.validation.BindException  

  14.  * 的解决方式.包括xml的配置  

  15.  * @author ZOUKANG  http://blog.csdn.net/kang89/ 

  16.  */  

  17. public class SpringDateConverter implements WebBindingInitializer {  

  18.   

  19.     @Override  

  20.     public void initBinder(WebDataBinder binder, WebRequest request) {  

  21.         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    

  22.         binder.registerCustomEditor(Date.classnew CustomDateEditor(df,true));    

  23.     }  

  24.   

  25. }  


关在srping 里声明

[html] view plaincopyprint?

  1. <bean  class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">    

  2.   <!-- 日期格式转换   -->  

  3.         <property name="webBindingInitializer">    

  4.             <bean class="com.ltkj.zhepg.com.util.SpringDateConverter" />    

  5.          </property>  

  6.  </bean>  

  7. <mvc:annotation-driven />  


现在数据也能绑定了,但就是ajax 提交后返回http 406 ,半天没有弄懂,后来想到了改为下面的声明配置即可,没有这个406问题

[html] view plaincopyprint?

  1. <bean  

  2.     class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  <!-- 这个类里面你可以注册拦截器什么的 -->  

  3. <bean id="jacksonMessageConverter"  

  4.     class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />  

  5. <bean  

  6.     class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  

  7.     <property name="webBindingInitializer">  

  8.         <bean class="com.ltkj.zhepg.com.util.SpringDateConverter" />  <!-- 这里注册自定义数据绑定类 -->  

  9.     </property>  

  10.     <property name="messageConverters">  

  11.         <list>  

  12.             <ref bean="jacksonMessageConverter" />    <!-- 注册JSON Converter -->  

  13.         </list>  

  14.     </property>  

  15. </bean>  

  16. <mvc:annotation-driven />  


附:415 Unsupported Media Type 没有配置<mvc:annotation-driven />转换
    406 the server responded with a status of 406 由于客户端请求的MediaType类型默认是:*/*

上面原因就在转为json没有显式声明。之前没有报406是因为没有使用自定义的转换器,json转换也采用了默认的了,所有没有这个406错误

[html] view plaincopyprint?

  1. @RequestMapping(value="/add"method=RequestMethod.POST)  

  2. public @ResponseBody Map<String, String> addCustomer( NotifyInfo notifyInfo, HttpServletRequest request) {  

  3.     Map<String, String> map = new HashMap<String, String>(1);  

  4.     try {  

  5.         if(notifyInfo.getContent() != null) {  

  6.             this.notifyInfoService.addOrUpdate(notifyInfo);  

  7.         }  

  8.         map.put(AJAX_MESSAGE, "true");  

  9.     } catch (ApplyException e) {  

  10.         map.put(AJAX_MESSAGE, "false");  

  11.     }  

  12.     return map;  




备注:406错误,spring方法映射适配器,对返回的list一些结果格式错误

<!-- 方法映射适配器 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="cacheSeconds" value="0" />
            <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
            </list>
        </property>
        <property name="webBindingInitializer">
            <bean class="framewrok.interceptors.MyWebBinding" />
        </property>
    </bean>


    <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>



public class MyWebBinding implements WebBindingInitializer {

    public void initBinder(WebDataBinder binder, WebRequest request) {
        // 1. 使用spring自带的CustomDateEditor
        // SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // binder.registerCustomEditor(Date.class, new
        // CustomDateEditor(dateFormat, true));
        //2. 自定义的PropertyEditorSupport
            binder.registerCustomEditor(Date.class, new DateConvertEditor());
    }

}