这篇文章主要介绍“SpringBoot表单提交全局日期格式转换器如何实现”,在日常操作中,相信很多人在SpringBoot表单提交全局日期格式转换器如何实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”SpringBoot表单提交全局日期格式转换器如何实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
分析
⏹当前台的提交数据的Content-Type
为以下情况
application/x-www-form-urlencoded
: 表单提交。
multipart/form-data
: 二进制流提交,多用于上传文件。
的时候,使用此转换方式。
⏹ 会用到全局日期转换工具类DateUtil.formatDateStrToDateAllFormat()
实现Spring
的Converter
接口,指定将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
注解会拦截所有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);
}
});
}
}
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表单提交全局日期格式转换器如何实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://blog.csdn.net/feyehong/article/details/130171176