自定義日期類型的數據綁定 前台 - 后台 或 后台 - 前台 互相轉換


第一,, 前台表單中,有一個日期 2014-03-11 提交到后台類型為date 時,會報一個轉換類錯誤 如下錯誤

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

因為springMVC不會自動轉換.

解決辦法 

[java] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. package com.lanyuan.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.  * spring3 mvc 的日期傳遞[前台-后台]bug:  
  13.  * org.springframework.validation.BindException  
  14.  * 的解決方式.包括xml的配置  
  15.  *  new SimpleDateFormat("yyyy-MM-dd");  這里的日期格式必須與提交的日期格式一致 
  16.  * @author lanyuan 
  17.  * Email:mmm333zzz520@163.com 
  18.  * date:2014-3-20 
  19.  */  
  20. public class SpringMVCDateConverter implements WebBindingInitializer {   
  21.   
  22.   public void initBinder(WebDataBinder binder, WebRequest request) {   
  23.       SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");     
  24.       binder.registerCustomEditor(Date.class, new CustomDateEditor(df,true));     
  25.   }   
  26.   
  27. }   

 

然后在springmvc的配置文件 spring-servlet.xml 上加一個段,  重點在於紅色字體


[html] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. <bean  
  2.         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  3.         <property name="messageConverters">  
  4.             <list>  
  5.                 <ref bean="mappingJacksonHttpMessageConverter" />  
  6.             </list>  
  7.         </property>  
  8.         <span style="color:#ff0000;"><property name="webBindingInitializer">  
  9.             <bean class="com.lanyuan.util.SpringMVCDateConverter" />  <!-- 這里注冊自定義數據綁定類 -->  
  10.         </property></span>  
  11.     </bean>  



第二: 后台回返前台時, 日期格式是Unix時間戳

例如 后台data : 2014-03-11 20:22:25  返回前台json的時間戳是1394508055  很明顯這不是我的要的結果, 我們需要的是 2014-03-11 20:22:25

解決辦法,在controller下新建這個類,然后在javabean的get方法上加上@JsonSerialize(using=JsonDateSerializer.class)

[java] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. package com.lanyuan.controller;  
  2.   
  3. import java.io.IOException;  
  4. import java.text.SimpleDateFormat;  
  5. import java.util.Date;  
  6.   
  7. import org.codehaus.jackson.JsonGenerator;  
  8. import org.codehaus.jackson.JsonProcessingException;  
  9. import org.codehaus.jackson.map.JsonSerializer;  
  10. import org.codehaus.jackson.map.SerializerProvider;  
  11. import org.springframework.stereotype.Component;  
  12.   
  13. /** 
  14.  * springMVC返回json時間格式化 
  15.  * 解決SpringMVC使用@ResponseBody返回json時,日期格式默認顯示為時間戳的問題。 
  16.  * 需要在get方法上加上@JsonSerialize(using=JsonDateSerializer.class) 
  17.  * @author lanyuan  
  18.  * Email:mmm333zzz520@163.com  
  19.  * date:2014-2-17 
  20.  */  
  21. @Component  
  22. public class JsonDateSerializer extends JsonSerializer<Date>{  
  23.     private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  24.     public void serialize(Date date, JsonGenerator gen, SerializerProvider provider) throws IOException, JsonProcessingException {  
  25.   
  26.         String formattedDate = dateFormat.format(date);  
  27.   
  28.         gen.writeString(formattedDate);  
  29.     }  
  30. }  


結語: 前台 - 后台  或 后台 - 前台 互相轉換 方法有多種,. 這些只是之一,供參考!

轉自http://blog.csdn.net/mmm333zzz/article/details/21696653

 

 

SpringMVC自定義日期類型的數據綁定

 

 

目錄:

 

  1. 應用場景
  2. 實現方法

 

[一]、應用場景

 

在實際應用中,經常會碰到表單中的日期 字符串和Javabean中的日期類型的屬性自動轉換,一般頁面輸入的日志格式為:yyyy-MM-dd ,而SpringMVC中默認不支持這樣的格式轉換,所以需要我們自定義數據類型的綁定才能實現這個功能。

 

[二]、實現方法

 

利用 WebBindingInitializer 注冊自定義日期轉換控制器。

 

自定義日期轉換器:MyDataBinding.java

 

 

Timestamp 的實現:CustomTimestampEditor.java 

 

 

修改spring-mvc 的配置文件,添加 webBindingInitializer 屬性的注入配置:

 

 

這樣就可以實現表單中的字符串自動轉換為Date或者Timestamp 類型。

 

本文介紹到此結束@Michael Sun.

 

轉自http://www.micmiu.com/j2ee/spring/springmvc-binding-date/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM