SpringMvc date数据传递处理

1、form表单提交date数据

form表单提交的date数据要在接收的字段上加入@DateTimeFormat注解

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createdAt;

2、后端传递date数据

2.1 、设置全局转换器

<mvc:annotation-driven>  
<!-- 处理responseBody 里面日期类型 -->  
    <mvc:message-converters>  
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
            <property name="objectMapper">  
                <bean class="com.fasterxml.jackson.databind.ObjectMapper">  
                    <property name="dateFormat">  
                        <bean class="java.text.SimpleDateFormat">  
                            <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />  
                        </bean>  
                    </property>  
                </bean>  
            </property>  
        </bean>  
    </mvc:message-converters>  
</mvc:annotation-driven>

2.2 、针对个别字段设置

@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
public Date getCreatetime() {
   return createtime;
}

2.3 、引入相关jar包

	<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.6.2</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.2</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.6.2</version>
    </dependency>
posted @ 2016-12-13 17:42  fengzp  阅读(5443)  评论(0编辑  收藏  举报