最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

參考資料

SpringBoot–LocalDateTime格式轉(zhuǎn)換(前端入?yún)?

SpringBoot @InitBinder注解綁定請求參數(shù)

分析

?當前臺的提交數(shù)據(jù)的Content-Type為以下情況

  • application/x-www-form-urlencoded: 表單提交。
  • multipart/form-data: 二進制流提交,多用于上傳文件。

的時候,使用此轉(zhuǎn)換方式。

? 會用到全局日期轉(zhuǎn)換工具類DateUtil.formatDateStrToDateAllFormat(),詳情可以參考 SpringBoot JSON全局日期格式轉(zhuǎn)換器

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

實現(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注解會攔截所有controller請求,配合@InitBinder注解,在參數(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表達式創(chuàng)建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 = {
	// ??待處理的日期字符串數(shù)據(jù)
    birthday: '20210105',
    nameAA: 'jiafeitian',
    hobby: '吃飯'
};

$.ajax({
    url: '后臺url',
    type: 'POST',
    // 對象轉(zhuǎn)換為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類型的數(shù)據(jù)
    private Date birthday;
}

??可以看到前臺提交的日期字符串被轉(zhuǎn)換為Date格式了

在這里插入圖片描述

到此這篇關(guān)于SpringBoot 表單提交全局日期格式轉(zhuǎn)換器的文章就介紹到這了,更多相關(guān)SpringBoot 全局日期格式轉(zhuǎn)換器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring?IOC中的組件掃描

    Spring?IOC中的組件掃描

    通過自動掃描,Spring?會自動從掃描指定的包及其子包下的所有類,并根據(jù)類上的特定注解將該類裝配到容器中,而無需在?XML?配置文件或?Java?配置類中逐一聲明每一個?Bean,這篇文章主要介紹了Spring?IOC中的組件掃描,需要的朋友可以參考下
    2022-05-05
  • Spring Cloud OAuth2中/oauth/token的返回內(nèi)容格式

    Spring Cloud OAuth2中/oauth/token的返回內(nèi)容格式

    Spring Cloud OAuth2 生成access token的請求/oauth/token的返回內(nèi)容就需要自定義,本文就詳細介紹一下,感興趣的可以了解一下
    2021-07-07
  • Java 泛型 Generic機制實例詳解

    Java 泛型 Generic機制實例詳解

    這篇文章主要為大家介紹了Java 泛型 Generic機制實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Java中特殊運算符及其應(yīng)用詳解

    Java中特殊運算符及其應(yīng)用詳解

    當涉及位操作和位級運算時,Java?提供了一組特殊的運算符,即左移(<<)和右移(>>)運算符,下面小編就帶大家深入了解一下它們的具體應(yīng)用吧
    2023-08-08
  • Java中常用的日期類圖文詳解

    Java中常用的日期類圖文詳解

    Java提供了Date類來處理日期、時間(此處的Date是指java.util包下的Date類,而不是java.sql包下的Date類),Date對象既包含日期,也包含時間,下面這篇文章主要給大家介紹了關(guān)于Java中常用的日期類的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • SpringBoot中隨機鹽值+雙重SHA256加密實戰(zhàn)

    SpringBoot中隨機鹽值+雙重SHA256加密實戰(zhàn)

    本文主要介紹了SpringBoot中隨機鹽值+雙重SHA256加密實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07
  • Java中DataInputStream和DataOutputStream的使用方法

    Java中DataInputStream和DataOutputStream的使用方法

    這篇文章主要介紹了Java中DataInputStream和DataOutputStream的使用方法,通過創(chuàng)建對象展開具體的內(nèi)容介紹,需要的小伙伴可以參考一下
    2022-05-05
  • Java中stream處理中map與flatMap的比較和使用案例

    Java中stream處理中map與flatMap的比較和使用案例

    這篇文章主要介紹了Java中stream處理中map與flatMap的比較和使用案例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 一文詳細講解Java時間格式轉(zhuǎn)換

    一文詳細講解Java時間格式轉(zhuǎn)換

    這篇文章主要介紹了Java時間格式轉(zhuǎn)換的相關(guān)資料,文中詳細介紹了SimpleDateFormat(適用于Java8之前)和java.time(適用于Java8及之后)的用法,需要的朋友可以參考下
    2024-12-12
  • Mybatis?SqlSession案例詳解

    Mybatis?SqlSession案例詳解

    這篇文章主要介紹了Mybatis?SqlSession詳解,本文我們講了如何創(chuàng)建SqlSession的幾個步驟,最后我們獲得一個DefaultSqlSession對象,里面包含了執(zhí)行器Executor和配置對象Configuration,需要的朋友可以參考下
    2023-04-04

最新評論

全南县| 怀化市| 蕉岭县| 鲁山县| 长汀县| 黔西| 缙云县| 隆尧县| 邳州市| 临澧县| 宣恩县| 徐闻县| 黄冈市| 泉州市| 通辽市| 平顺县| 大理市| 通山县| 瓦房店市| 南城县| 玉田县| 易门县| 沙洋县| 霍山县| 浦东新区| 霸州市| 洛浦县| 连江县| 丰原市| 韶山市| 侯马市| 西充县| 阿拉善盟| 阿勒泰市| 远安县| 沈阳市| 冕宁县| 申扎县| 乌拉特前旗| 长汀县| 天全县|