SpringBoot表单提交全局日期格式转换器如何实现

这篇文章主要介绍“SpringBoot表单提交全局日期格式转换器如何实现”,在日常操作中,相信很多人在SpringBoot表单提交全局日期格式转换器如何实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”SpringBoot表单提交全局日期格式转换器如何实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

分析

⏹当前台的提交数据的Content-Type为以下情况

  • application/x-www-form-urlencoded: 表单提交。

  • multipart/form-data: 二进制流提交,多用于上传文件。

的时候,使用此转换方式。

⏹ 会用到全局日期转换工具类DateUtil.formatDateStrToDateAllFormat()

一. 实现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> {

    @Override
    public 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 {

    @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表达式创建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表单提交全局日期格式转换器如何实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注蜗牛博客网站,小编会继续努力为大家带来更多实用的文章!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接