在Spring MVC中存在两大类的类型转换,一类是Json,一个是Spring的Binder转换。
JSON:
使用Json转换时,可以如下使用:
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; } }
第一个@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())); } } }
/** * Description: 日期转换 - "yyyy-MM-dd HH:mm:ss" * Author: liuzh * Update: liuzh(2014-04-17 10:59) */ public class DateYMDHMSJsonDeserializer extends JsonDeserializer<Date> { @Override public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { try { return DateUtil.formatStringToDate(jp.getText(), DateUtil.DATE_FORMAT_TIME_T); } catch (BusinessException e) { return new Date(jp.getLongValue()); } } }
Binder:
这种类型转换的时候,使用的是Spring的参数绑定,代码如下:
/** * Description: 全局类型转换 * Author: liuzh * 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); } } } /** * 转换为日期字符串 */ @Override public String getAsText() { Date value = (Date) getValue(); String dateStr = null; if (value == null) { return ""; } else { try { 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; } catch (Exception ex) { throw new IllegalArgumentException("转换日期失败: " + ex.getMessage(), ex); } } } } @Override public void initBinder(WebDataBinder binder, WebRequest request) { //日期格式转换 binder.registerCustomEditor(Date.class, new SmartDateEditor()); } }
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="webBindingInitializer"> <bean class="com.easternie.sys.common.GlobalDataBinder"/> </property> </bean>