SpringBoot?表單提交全局日期格式轉(zhuǎn)換器實(shí)現(xiàn)方式
參考資料
SpringBoot–LocalDateTime格式轉(zhuǎn)換(前端入?yún)?
SpringBoot @InitBinder注解綁定請(qǐng)求參數(shù)
分析
?當(dāng)前臺(tái)的提交數(shù)據(jù)的Content-Type為以下情況
application/x-www-form-urlencoded: 表單提交。multipart/form-data: 二進(jìn)制流提交,多用于上傳文件。
的時(shí)候,使用此轉(zhuǎn)換方式。
? 會(huì)用到全局日期轉(zhuǎn)換工具類(lèi)DateUtil.formatDateStrToDateAllFormat(),詳情可以參考 SpringBoot JSON全局日期格式轉(zhuǎn)換器
一. 實(shí)現(xiàn)Converter<S, T>接口的方式
實(shí)現(xiàn)Spring的Converter接口,指定將String轉(zhuǎn)換為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注解會(huì)攔截所有controller請(qǐng)求,配合@InitBinder注解,在參數(shù)封裝到實(shí)體類(lèi)之前將String日期轉(zhuǎn)換為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) {
// 通過(guò)lombda表達(dá)式創(chuàng)建WebBindingInitializer對(duì)象
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;
}
}四. 效果
?前臺(tái)JS
const jsonData = {
// ??待處理的日期字符串?dāng)?shù)據(jù)
birthday: '20210105',
nameAA: 'jiafeitian',
hobby: '吃飯'
};
$.ajax({
url: '后臺(tái)url',
type: 'POST',
// 對(duì)象轉(zhuǎn)換為json字符串
data: jsonData,
// 指定為表單提交
contentType: "application/x-www-form-urlencoded",
success: function (data, status, xhr) {
console.log(data);
}
});?后臺(tái)Form
import lombok.Data;
import java.util.Date;
@Data
public class Test15Form {
private String name;
private String hobby;
private String address;
// 用來(lái)接收的Date類(lèi)型的數(shù)據(jù)
private Date birthday;
}??可以看到前臺(tái)提交的日期字符串被轉(zhuǎn)換為Date格式了

到此這篇關(guān)于SpringBoot 表單提交全局日期格式轉(zhuǎn)換器的文章就介紹到這了,更多相關(guān)SpringBoot 全局日期格式轉(zhuǎn)換器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Cloud OAuth2中/oauth/token的返回內(nèi)容格式
Spring Cloud OAuth2 生成access token的請(qǐng)求/oauth/token的返回內(nèi)容就需要自定義,本文就詳細(xì)介紹一下,感興趣的可以了解一下2021-07-07
Java 泛型 Generic機(jī)制實(shí)例詳解
這篇文章主要為大家介紹了Java 泛型 Generic機(jī)制實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
SpringBoot中隨機(jī)鹽值+雙重SHA256加密實(shí)戰(zhàn)
本文主要介紹了SpringBoot中隨機(jī)鹽值+雙重SHA256加密實(shí)戰(zhàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
Java中DataInputStream和DataOutputStream的使用方法
這篇文章主要介紹了Java中DataInputStream和DataOutputStream的使用方法,通過(guò)創(chuàng)建對(duì)象展開(kāi)具體的內(nèi)容介紹,需要的小伙伴可以參考一下2022-05-05
Java中stream處理中map與flatMap的比較和使用案例
這篇文章主要介紹了Java中stream處理中map與flatMap的比較和使用案例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
一文詳細(xì)講解Java時(shí)間格式轉(zhuǎn)換
這篇文章主要介紹了Java時(shí)間格式轉(zhuǎn)換的相關(guān)資料,文中詳細(xì)介紹了SimpleDateFormat(適用于Java8之前)和java.time(適用于Java8及之后)的用法,需要的朋友可以參考下2024-12-12

