zoukankan      html  css  js  c++  java
  • SpringBoot 日期空字符串转换失败处理

    方式1 Controller处理>

        @InitBinder
        public void initBinder(WebDataBinder binder, WebRequest request) {
    
            //转换日期
            DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
            binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
        }
    

      方式2 全局处理>

    package sy.ccpit.qiye.config;
    
    import org.springframework.beans.propertyeditors.CustomDateEditor;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.format.support.FormattingConversionService;
    import org.springframework.validation.Validator;
    import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
    
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    /**
     * @Title: CustomDateConfig
     * @ProjectName qiye
     * @date 2019/12/1013:14
     */
    @Configuration
    public class CustomDateConfig{
    
        @Bean
        public ConfigurableWebBindingInitializer configurableWebBindingInitializer(FormattingConversionService mvcConversionService, Validator mvcValidator) {
            ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
            initializer.setConversionService(mvcConversionService);
            initializer.setValidator(mvcValidator);
            DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
            //装配自定义属性编辑器
            initializer.setPropertyEditorRegistrar(propertyEditorRegistry -> {
                propertyEditorRegistry.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
            });
            return initializer;
        }
    }
    

      

  • 相关阅读:
    每日随笔
    每日随笔
    每日随笔
    JavaSE 类加载机制
    MySQL 索引
    JavaSE 线程的生命周期
    JVM堆内存(heap)详解
    Redis 学习笔记(三)哨兵模式配置高可用和集群
    sqlserver数据库连接数相关问题
    多项式多点求值
  • 原文地址:https://www.cnblogs.com/412013cl/p/12016087.html
Copyright © 2011-2022 走看看