自定义日期类型的数据绑定 前台 - 后台 或 后台 - 前台 互相转换
2016-05-17 00:00
513 查看
第一,, 前台表单中,有一个日期 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
package com.lanyuan.util;
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的配置
* new SimpleDateFormat("yyyy-MM-dd"); 这里的日期格式必须与提交的日期格式一致
* @author lanyuan
* Email:mmm333zzz520@163.com
* date:2014-3-20
*/
public class SpringMVCDateConverter implements WebBindingInitializer {
public void initBinder(WebDataBinder binder, WebRequest request) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(df,true));
}
}
然后在springmvc的配置文件 spring-servlet.xml 上加一个段, 重点在于红色字体
[html]
view plain
copy
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
<span style="color:#ff0000;"><property name="webBindingInitializer">
<bean class="com.lanyuan.util.SpringMVCDateConverter" /> <!-- 这里注册自定义数据绑定类 -->
</property></span>
</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
package com.lanyuan.controller;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.springframework.stereotype.Component;
/**
* springMVC返回json时间格式化
* 解决SpringMVC使用@ResponseBody返回json时,日期格式默认显示为时间戳的问题。
* 需要在get方法上加上@JsonSerialize(using=JsonDateSerializer.class)
* @author lanyuan
* Email:mmm333zzz520@163.com
* date:2014-2-17
*/
@Component
public class JsonDateSerializer extends JsonSerializer<Date>{
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider) throws IOException, JsonProcessingException {
String formattedDate = dateFormat.format(date);
gen.writeString(formattedDate);
}
}
结语: 前台 - 后台 或 后台 - 前台 互相转换 方法有多种,. 这些只是之一,供参考!
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
package com.lanyuan.util;
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的配置
* new SimpleDateFormat("yyyy-MM-dd"); 这里的日期格式必须与提交的日期格式一致
* @author lanyuan
* Email:mmm333zzz520@163.com
* date:2014-3-20
*/
public class SpringMVCDateConverter implements WebBindingInitializer {
public void initBinder(WebDataBinder binder, WebRequest request) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(df,true));
}
}
然后在springmvc的配置文件 spring-servlet.xml 上加一个段, 重点在于红色字体
[html]
view plain
copy
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
<span style="color:#ff0000;"><property name="webBindingInitializer">
<bean class="com.lanyuan.util.SpringMVCDateConverter" /> <!-- 这里注册自定义数据绑定类 -->
</property></span>
</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
package com.lanyuan.controller;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.springframework.stereotype.Component;
/**
* springMVC返回json时间格式化
* 解决SpringMVC使用@ResponseBody返回json时,日期格式默认显示为时间戳的问题。
* 需要在get方法上加上@JsonSerialize(using=JsonDateSerializer.class)
* @author lanyuan
* Email:mmm333zzz520@163.com
* date:2014-2-17
*/
@Component
public class JsonDateSerializer extends JsonSerializer<Date>{
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider) throws IOException, JsonProcessingException {
String formattedDate = dateFormat.format(date);
gen.writeString(formattedDate);
}
}
结语: 前台 - 后台 或 后台 - 前台 互相转换 方法有多种,. 这些只是之一,供参考!
相关文章推荐
- 中文首字母 排序
- Mybatis极其(最)简(好)单(用)的一个分页插件
- Spring与JMX集成
- 别怕!手把手教你使用Git
- Java 正则表达式详解
- java package和import的理解
- 利用100行代码动态创建并部署流程
- 中心极限定理及其应用
- 【题解】PAT.团体程序设计天梯赛.L3-004.肿瘤诊断
- c#优化
- PHP基本知识
- 【C#进阶系列】27 I/O限制的异步操作
- 深入了解MyBatis二级缓存
- 通过JAVA在命令行(如控制台)运行Shell指令
- Lucene5学习之LuceneUtils工具类简单封装
- maven 错误解决办法集
- MyBatis直接执行SQL的工具SqlMapper
- 经典算法——堆排序笔试题
- 构造AJAX参数, 表单元素JSON相互转换
- js遍历jsonTree