zoukankan      html  css  js  c++  java
  • Spring MVC对日期处理的不友好问题

    一、后台日期类型解析到前端

    1.在springmvc的配置文件中添加这个.annotation-driven在配置文件中只配置一次     (此方法全局作用)
    <mvc:annotation-driven>
    <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.直接在字段上写上  
    @JsonFormat(pattern="yyyy-MM-dd",timezone="GMT-8")
    private Date inputtime;

    二、前端日期类型传到后台

    1.在Controller类中添加这个方法      (此方法全局作用)
    @InitBinder
    public void initBinder(WebDataBinder binder){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }


    2.直接在字段上添加
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date beginDate;

  • 相关阅读:
    日志模块
    模块介绍3
    模块介绍2
    模块介绍
    迭代器
    Python装饰器续/三元表达式/匿名函数
    Python装饰器详解
    LATEX LIAN XI
    BELLMAN 最短路算法
    B阿狸和桃子的游戏
  • 原文地址:https://www.cnblogs.com/weixupeng/p/8553965.html
Copyright © 2011-2022 走看看