广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot 表单提交全局日期格式转换器实现方式
  • 897
分享到

SpringBoot 表单提交全局日期格式转换器实现方式

Python 官方文档:入门教程 => 点击学习

摘要

目录参考资料一. 实现Converter<S, T>接口的方式二. 全局@ControllerAdvice + @InitBinder注解的方式三. RequestMap

参考资料

SpringBoot–LocalDateTime格式转换(前端入参)

springBoot @InitBinder注解绑定请求参数

分析

⏹当前台的提交数据的Content-Type为以下情况

  • application/x-www-fORM-urlencoded: 表单提交。
  • multipart/form-data: 二进制流提交,多用于上传文件。

的时候,使用此转换方式。

⏹ 会用到全局日期转换工具DateUtil.formatDateStrToDateAllFormat(),详情可以参考 SpringBoot JSON全局日期格式转换器

一. 实现Converter<S, T>接口的方式

实现SpringConverter接口,指定将String转换为Date

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class GlobalFormStrToDateConvert implements Converter<String, Date> {

    @Override
    public Date convert(String dateStr) {
        try {
            return DateUtil.formatDateStrToDateAllFormat(dateStr);
        } catch (Exception e) {
            return null;
        }
    }
}

二. 全局@ControllerAdvice + @InitBinder注解的方式

@ControllerAdvice注解会拦截所有controller请求,配合@InitBinder注解,在参数封装到实体类之前将String日期转换为Date日期

import org.springframework.WEB.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;
import java.util.Date;

@ControllerAdvice
public class GlobalFormStrToDateConvert {

    @InitBinder
    protected void dateStrToDate(WebDataBinder binder) {

        binder.reGISterCustomEditor(Date.class, new PropertyEditorSupport() {

            @Override
            public void setAsText(String dateStr) throws IllegalArgumentException {
                Date date = DateUtil.formatDateStrToDateAllFormat(dateStr);
                setValue(date);
            }
        });
    }
}

三. RequestMappingHandlerAdapter的方式

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import java.beans.PropertyEditorSupport;
import java.util.Date;

@Configuration
public class GlobalFormStrToDateConvert {

    @Bean
    public RequestMappingHandlerAdapter webBindingInitializer(RequestMappingHandlerAdapter requestMappingHandlerAdapter) {
		
		// 通过lombda表达式创建WebBindingInitializer对象
        WebBindingInitializer webBindingInitializer = binder -> binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String dateStr) {
                Date date = DateUtil.formatDateStrToDateAllFormat(dateStr);
                setValue(date);
            }
        });
        requestMappingHandlerAdapter.setWebBindingInitializer(webBindingInitializer);
        return requestMappingHandlerAdapter;
    }
}

四. 效果

⏹前台js

const jsonData = {
	// 👉待处理的日期字符串数据
    birthday: '20210105',
    nameAA: 'jiafeitian',
    hobby: '吃饭'
};

$.ajax({
    url: '后台url',
    type: 'POST',
    // 对象转换为json字符串
    data: jsonData,
    // 指定为表单提交
    contentType: "application/x-www-form-urlencoded",
    success: function (data, status, xhr) {
        console.log(data);
    }
});

⏹后台Form

import lombok.Data;
import java.util.Date;

@Data
public class Test15Form {

    private String name;

    private String hobby;

    private String address;
	
	// 用来接收的Date类型的数据
    private Date birthday;
}

👇可以看到前台提交的日期字符串被转换为Date格式了

在这里插入图片描述

到此这篇关于SpringBoot 表单提交全局日期格式转换器的文章就介绍到这了,更多相关SpringBoot 全局日期格式转换器内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringBoot 表单提交全局日期格式转换器实现方式

本文链接: https://www.lsjlt.com/news/210006.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作