1,通用处理
每个ajax返回的日期类型的数据都会按照这个格式处理
<mvc:annotation-driven>
<mvc:message-converters>
<ref bean="jacksonMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>text/plain;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<property name="objectMapper" ref="objectMapper" />
</bean>
<!-- 日期的格式化 -->
<bean id="fullDateTime" class="java.text.SimpleDateFormat">
<constructor-arg>
<value>yyyy-MM-dd HH:mm:ss</value>
</constructor-arg>
</bean>
<!-- 对象类型的输出,被MySpringContextHolder引用-->
<bean id="objectMapper" class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<ref bean="fullDateTime" />
</property>
<property name="serializationInclusion">
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
</bean>2,个别特殊处理
如果有日期类型的格式与通用类型不一样,可以在bean的get方法中增加如下定义
@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
public Date getRegDate() {
return regDate;
}
本文介绍了一种通过配置Spring MVC的转换器来统一处理Ajax响应中日期格式的方法,并提供了针对特殊情况的自定义日期格式处理方案。

被折叠的 条评论
为什么被折叠?



