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;

  • 相关阅读:
    Redis服务器配置
    Spark History Server配置使用
    CentOS7.3安装Nginx
    U盘安装CentOS7的最终解决方案
    iconfont_3种引用方式
    div+css 让一个小div在另一个大div里面 垂直居中
    JavaScript数组方法
    addEventListener()和removeEventListener()
    js获取网页高度
    Linux修改命令行样式
  • 原文地址:https://www.cnblogs.com/weixupeng/p/8553965.html
Copyright © 2011-2022 走看看