亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot?表單提交全局日期格式轉(zhuǎn)換器實(shí)現(xiàn)方式

 更新時(shí)間:2023年04月17日 11:15:31   作者:fengyehongWorld  
這篇文章主要介紹了SpringBoot?表單提交全局日期格式轉(zhuǎn)換器,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

參考資料

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)換工具類DateUtil.formatDateStrToDateAllFormat(),詳情可以參考 SpringBoot JSON全局日期格式轉(zhuǎn)換器

一. 實(shí)現(xiàn)Converter<S, T>接口的方式

實(shí)現(xiàn)SpringConverter接口,指定將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í)體類之前將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) {
		
		// 通過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;
	
	// 用來接收的Date類型的數(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中容器的創(chuàng)建流程詳細(xì)解讀

    Spring中容器的創(chuàng)建流程詳細(xì)解讀

    這篇文章主要介紹了Spring中容器的創(chuàng)建流程詳細(xì)解讀,Spring?框架其本質(zhì)是作為一個(gè)容器,提供給應(yīng)用程序需要的對(duì)象,了解容器的誕生過程,有助于我們理解?Spring?框架,也便于我們“插手”這個(gè)過程,需要的朋友可以參考下
    2023-10-10
  • java中文傳值亂碼問題的解決方法

    java中文傳值亂碼問題的解決方法

    這篇文章主要為大家詳細(xì)介紹了java中文傳值亂碼問題的解決方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • 使用gRPC微服務(wù)的內(nèi)部通信優(yōu)化

    使用gRPC微服務(wù)的內(nèi)部通信優(yōu)化

    這篇文章主要為大家介紹了微服務(wù)優(yōu)化之使用gRPC做微服務(wù)的內(nèi)部通信,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • Java并發(fā)編程之顯式鎖機(jī)制詳解

    Java并發(fā)編程之顯式鎖機(jī)制詳解

    這篇文章主要為大家詳細(xì)介紹了Java并發(fā)編程之顯式鎖機(jī)制的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Java RabbitMQ 中的消息長(zhǎng)期不消費(fèi)會(huì)過期嗎

    Java RabbitMQ 中的消息長(zhǎng)期不消費(fèi)會(huì)過期嗎

    RabbitMQ支持消息的過期時(shí)間,在消息發(fā)送時(shí)可以進(jìn)行指定。 RabbitMQ支持隊(duì)列的過期時(shí)間,從消息入隊(duì)列開始計(jì)算,只要超過了隊(duì)列的超時(shí)時(shí)間配置,那么消息會(huì)自動(dòng)的清除
    2021-09-09
  • 使用okhttp替換Feign默認(rèn)Client的操作

    使用okhttp替換Feign默認(rèn)Client的操作

    這篇文章主要介紹了使用okhttp替換Feign默認(rèn)Client的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • JAVA中使用雙括號(hào)來初始化靜態(tài)常量的小技巧

    JAVA中使用雙括號(hào)來初始化靜態(tài)常量的小技巧

    這篇文章主要介紹了JAVA中使用雙括號(hào)來初始化靜態(tài)常量的小技巧,需要的朋友可以參考下
    2014-06-06
  • Java實(shí)現(xiàn)將PDF轉(zhuǎn)為PDF/A

    Java實(shí)現(xiàn)將PDF轉(zhuǎn)為PDF/A

    通過將PDF格式轉(zhuǎn)換為PDF/A格式,可保護(hù)文檔布局、格式、字體、大小等不受更改,從而實(shí)現(xiàn)文檔安全保護(hù)的目的,同時(shí)又能保證文檔可讀、可訪問。本文將為大家介紹如何實(shí)現(xiàn)這一轉(zhuǎn)換,需要的可以參考一下
    2022-01-01
  • java對(duì)象list使用stream根據(jù)某一個(gè)屬性轉(zhuǎn)換成map的3種方式舉例

    java對(duì)象list使用stream根據(jù)某一個(gè)屬性轉(zhuǎn)換成map的3種方式舉例

    開發(fā)小伙伴們通常會(huì)需要使用到對(duì)象和Map互相轉(zhuǎn)換的開發(fā)場(chǎng)景,下面這篇文章主要給大家介紹了關(guān)于java對(duì)象list使用stream根據(jù)某一個(gè)屬性轉(zhuǎn)換成map的3種方式,需要的朋友可以參考下
    2024-01-01
  • MyBatis-plus數(shù)據(jù)庫(kù)字段排序不準(zhǔn)確的解決

    MyBatis-plus數(shù)據(jù)庫(kù)字段排序不準(zhǔn)確的解決

    這篇文章主要介紹了MyBatis-plus數(shù)據(jù)庫(kù)字段排序不準(zhǔn)確的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02

最新評(píng)論