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

news/2024/10/17 16:32:44/

参考资料

  1. SpringBoot–LocalDateTime格式转换(前端入参)
  2. SpringBoot @InitBinder注解绑定请求参数

目录

  • 一. 实现Converter<S, T>接口的方式
  • 二. 全局@ControllerAdvice + @InitBinder注解的方式
  • 三. RequestMappingHandlerAdapter的方式
  • 四. 效果


分析
⏹当前台的提交数据的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> {@Overridepublic 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 {@InitBinderprotected void dateStrToDate(WebDataBinder binder) {binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {@Overridepublic 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 {@Beanpublic RequestMappingHandlerAdapter webBindingInitializer(RequestMappingHandlerAdapter requestMappingHandlerAdapter) {// 通过lombda表达式创建WebBindingInitializer对象WebBindingInitializer webBindingInitializer = binder -> binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {@Overridepublic 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格式了

在这里插入图片描述


http://www.ppmy.cn/news/47250.html

相关文章

计及氢能的综合能源优化调度研究(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

AI数据标注工程师这个职业怎么样?

本篇文章主要讲解ai数据标注工程师这个职业的具体情况和相关的职业前景 作者&#xff1a;任聪聪 日期&#xff1a;2023年4月18日 数据是ai的灵魂&#xff0c;自然界中相对应的数据都活多少存在不准确、杂乱、无效等属性&#xff0c;需要人为进行收集、整理、分类和处理。其中ai…

最优化方法Python计算:连续函数的单峰区间计算

我们知道&#xff0c;闭区间上的一元连续函数必在区间上取得最大值和最小值。实践中我们需要能数值地确定含有 f ( x ) f(x) f(x)的唯一最优解 x 0 x_0 x0​的区间 [ a , b ] [a,b] [a,b]。这里介绍寻求连续函数 f ( x ) f(x) f(x)在一点 x ∗ x^* x∗附近单峰区间的包围算法及…

学习小程序基础内容之逻辑交互

我们先来看一下实现的效果。 然后再来分享结构。 结构分为左右3:7 分配&#xff0c; 左侧是类别&#xff0c;右侧是该类别对应的品牌。 后台会在onload的请求把左侧的类别返回来&#xff0c;然后我们通过循环把数据展示出来。然后通过点击事件&#xff0c;把对应的品牌请求回来…

学习同步异步的概念,并了解MQ消息队列

文章目录 一、 同步和异步1.1 同步调用1.2 异步调用 二、MQ1.1 介绍1.2 MQ的优点和使用场景 一、 同步和异步 1.1 同步调用 同步调用是一种程序调用方式&#xff0c;在该调用方式中&#xff0c;调用者发起一个请求&#xff0c;然后一直等待被调用者返回响应结果后再继续执行。…

《Android性能优化》一次失败的启动速度优化

正文 在优化APP启动之前&#xff0c;我们首先需要知道&#xff0c;APP启动时究竟发生了什么&#xff0c;才能有的放矢的优化。 APP的启动过程 APP的启动过程就是指&#xff0c;在手机屏幕上点击某个APP的图标&#xff0c;到APP的首页显示在用户面前的过程。 一般APP的启动过…

【数据分析之道-NumPy(三)】numpy切片与索引

文章目录 专栏导读1、前言2、NumPy数组切片2.1一维数组切片2.2多维数组切片 3、NumPy数组索引3.1一维数组索引3.2多维数组索引 4、NumPy数组高级索引4.1整数数组索引4.2布尔数组索引4.3数组索引 总结 专栏导读 ✍ 作者简介&#xff1a;i阿极&#xff0c;CSDN Python领域新星创作…

【一】MATLAB基础知识

【一】MATLAB基础知识 1 数值数据类型的分类 整型 无符号整数&#xff1a;无符号8位整数、无符号16位整数、无符号32位整数、 无符号64位整数。 带符号整数&#xff1a;带符号8位整数、带符号16位整数、带符号32位整数、 带符号64位整数。 无符号8位整数数据范围&#xff…