自定义日期类型的数据绑定 前台 - 后台 或 后台 - 前台 互相转换

第一,, 前台表单中,有一个日期 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.classnew 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. }  


结语: 前台 - 后台  或 后台 - 前台 互相转换 方法有多种,. 这些只是之一,供参考!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值