类型有很多,这里只用日期为例说明。
在spring mvc中存在两大类的类型转换,一类是json,一个是spring的binder转换。
json:
使用json转换时,可以如下使用:
[java] view
plain copy
public class test {
private date createdate;
@jsonserialize(using = dateymdhmsjsonserializer.class)
public date getcreatedate() {
return createdate;
}
@jsondeserialize(using = dateymdhmsjsondeserializer.class)
public void setcreatedate(date createdate) {
this.createdate = createdate;
}
可以看到这里使用了两个json转换的注解:
第一个@jsonserialize是转换为字符串,主要是后台传递给前台时的日期格式;
第二个@jsondeserialize是转换字符串为日期类型,主要是从前台往后台传递时的日期。
两个具体转换类的实现:
/**
* description: 日期转换 - "yyyy-mm-dd hh:mm:ss"
* author: liuzh
* update: liuzh(2014-04-17 10:59)
*/
public class dateymdhmsjsonserializer extends jsonserializer<date>{
@override
public void serialize(date date, jsongenerator jsongenerator, serializerprovider serializerprovider) throws ioexception, jsonprocessingexception {
try {
jsongenerator.writestring(dateutil.formatdate(date, dateutil.date_format_time_t));
} catch (businessexception e) {
jsongenerator.writestring(string.valueof(date.gettime()));
}
public class dateymdhmsjsondeserializer extends jsondeserializer<date> {
public date deserialize(jsonparser jp, deserializationcontext ctxt) throws ioexception, jsonprocessingexception {
return dateutil.formatstringtodate(jp.gettext(), dateutil.date_format_time_t);
return new date(jp.getlongvalue());
其中dateutil是一个对日期格式转换的工具类,使用的simpledateformat进行转换。
binder:
这种类型转换的时候,使用的是spring的参数绑定,代码如下:
* description: 全局类型转换
* update: liuzh(2014-05-26 13:08)
public class globaldatabinder implements webbindinginitializer {
/**
* 智能日期转换,针对四种格式日期:
* 1.2014-05-26
* 2.1401951570548
* 3.2014-05-26 00:00
* 4.2014-05-26 00:00:00
*/
private class smartdateeditor extends propertyeditorsupport {
/**
* 根据2014-05-26 00:00:00长度来判断选择哪种转换方式
*/
@override
public void setastext(string text) throws illegalargumentexception {
if (text == null || text.length() == 0) {
setvalue(null);
} else {
try {
if (text.length() == 10) {
setvalue(dateutil.formatstringtodate(text, dateutil.date_format_yyyymmdd));
} else if (text.length() == 13) {
setvalue(new date(long.parselong(text)));
} else if (text.length() == 16) {
setvalue(dateutil.formatstringtodate(text, dateutil.date_format_time_r));
} else if (text.length() == 19) {
setvalue(dateutil.formatstringtodate(text, dateutil.date_format_time_t));
} else {
throw new illegalargumentexception("转换日期失败: 日期长度不符合要求!");
}
} catch (exception ex) {
throw new illegalargumentexception("转换日期失败: " + ex.getmessage(), ex);
}
}
* 转换为日期字符串
public string getastext() {
date value = (date) getvalue();
string datestr = null;
if (value == null) {
return "";
datestr = dateutil.formatdate(value, dateutil.date_format_time_t);
if (datestr.endswith(" 00:00:00")) {
datestr = datestr.substring(0, 10);
} else if (datestr.endswith(":00")) {
datestr = datestr.substring(0, 16);
return datestr;
public void initbinder(webdatabinder binder, webrequest request) {
//日期格式转换
binder.registercustomeditor(date.class, new smartdateeditor());
这里对日期类型进行了一些判断来特殊处理。该类需要在spring的xml进行配置:
[html] view
<bean class="org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter">
<property name="webbindinginitializer">
<bean class="com.easternie.sys.common.globaldatabinder"/>
</property>
</bean>
通过这种配置之后,spring就能对日期进行智能转换了。