SpringBoot @InitBinder注解绑定请求参数

news2024/9/22 21:20:11

参考资料

  1. springMVC之@InitBinder 和 Validator
  2. springMVC之@InitBinder的用法1
  3. springMVC之@InitBinder的用法2

目录

  • 一. 作用
  • 二. 前期准备
  • 三. Get请求 + URL传值处理
    • 3.1 前台-test16.html
    • 3.2 Controller层
    • 3.3 效果
  • 四. Post请求 + 表单传值 + 自定义日期属性绑定器
    • 4.1 前台-test16.html
    • 4.2 form实体类
    • 4.3 Controller层
    • 4.4 效果
  • 五. 其他自定义属性编辑器实例
    • 5.1 自定义SexPropertyEditor
    • 5.2 自定义StringToListPropertyEditor
    • 5.3 form实体类
    • 5.4 前端
    • 5.5 Controller层
    • 5.6 效果
  • 六. 多个@InitBinder注解修饰的方法
  • 七. 其他用法


一. 作用

作用于Controller层中,在Controller层的方法执行前执行,主要作用是初始化当前Controller层的数据绑定器(或者属性绑定器),帮助完成数据处理和数据绑定。

被该注解修饰的方法会有一个形参WebDataBinder,可以帮我们将request请求中的参数处理绑定到JavaBean中。

在这里插入图片描述

二. 前期准备

import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;

@Data
public class Test16Form {

    private String name;

    private String sex;

    private Date birthday;

    private BigDecimal money;
}

三. Get请求 + URL传值处理

3.1 前台-test16.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <button id="getBtn">发送get请求</button><br>
</div>
</body>
<script type="text/javascript" th:src="@{/js/public/jquery-3.6.0.min.js}"></script>
<script>
    $("#getBtn").click(function() {
        
        const urlSearchParams = new URLSearchParams();
        // 👉含有空格
        urlSearchParams.append("name", "贾飞天   ");
        urlSearchParams.append("sex", "男");
        // 👉值为yyyy-MM-dd HH:mm:ss格式的日期字符串
        urlSearchParams.append("birthday", "2022-11-11 12:12:12");
        urlSearchParams.append("money", "10000");

        const url = `/test16/receiveGet?${urlSearchParams.toString()}`;
        $.ajax({
            url,
            type: 'GET',
            success: function (data, status, xhr) {
                console.log(data);
            }
        });
    });
</script>
</html>

3.2 Controller层

  • StringTrimmerEditorCustomDateEditor是框架自带的属性处理器
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/test16")
public class Test16Controller {

    @InitBinder
    public void formBinder(WebDataBinder binder) {

        // 只要是String类型,就去除字符串前后的空格
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));

        // 只有当属性名为birthday且为Date类型的才使用使用框架自带的CustomDateEditor编辑器将String处理为Date
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, "birthday", new CustomDateEditor(df, true));
    }

    @GetMapping("/init")
    public ModelAndView init() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("test16");
        return modelAndView;
    }

    @GetMapping("/receiveGet")
    @ResponseBody
    public void receiveGet(Test16Form form) {

        System.out.println(form);
    }
}

3.3 效果

  • 字符串两端的空格被去除
  • String格式的日期被转换为Date格式的日期
    在这里插入图片描述

四. Post请求 + 表单传值 + 自定义日期属性绑定器

4.1 前台-test16.html

  • 表单提交的数据若包含List<实体类>这种数据结构
    在前台需要用 form对应的属性名[下标].实体类属性名 这种方式准备数据
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <button id="postBtn">发送post请求</button><br>
</div>
</body>
<script type="text/javascript" th:src="@{/js/public/jquery-3.6.0.min.js}"></script>
<script>
    $("#postBtn").click(function() {

        const paramObj = {
            name: "贾飞天    ",
            sex: '不明',
            money: "10000",
            // yyyy-MM-dd HH:mm:ss格式
            birthday: '2022-11-11 12:12:12',
            // 后台中的List实体类区域
            "tableList[0].id": 1,
            "tableList[0].address": '测试address    ',
            "tableList[0].hobby": '测试hobby     ',
            // yyyy年MM月dd日 HH:mm:ss格式
            "tableList[0].workDate": '2022年11月11日 14:14:14',
        };

        $.ajax({
            url: `/test16/receivePost`,
            type: 'POST',
            data: paramObj,
            // 表单格式提交
            contentType : 'application/x-www-form-urlencoded;charset=utf-8',
            // 后端返回给前端的数据类型
            dataType: 'json',
            success: function (data, status, xhr) {
                console.log(data);
            }
        });
    });
</script>
</html>

4.2 form实体类

import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Data
public class Test16Form {

    private String name;

    private String sex;
	
	// 待转换类型
    private Date birthday;

    private BigDecimal money;

    private List<Test4Entity> tableList;
}
import lombok.Data;
import java.util.Date;

@Data
public class Test4Entity {

    private String id;

    private String address;

    private String hobby;
	
	// 待转换类型
    private Date workDate;
}

4.3 Controller层

  • 我们可以通过PropertyEditorSupport类来实现我们自己的属性编辑器
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.beans.PropertyEditorSupport;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/test16")
public class Test16Controller {

    @InitBinder
    public void formBinder(WebDataBinder binder) {

        // 只要是String类型,就去除字符串前后的空格
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));

        // 自定义日期转换属性处理器
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {

            @Override
            public void setAsText(String dateStr) {
                DateFormat dateFormat = null;
                try {
                    if (ObjectUtils.isEmpty(dateStr)) {
                        setValue(dateStr);
                        return;
                    }

                    // yyyy-MM-dd HH:mm:ss格式
                    if (dateStr.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
                        dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    }
                    // yyyy年MM月dd日 HH:mm:ss格式
                    else if (dateStr.matches("^\\d{4}年\\d{1,2}月\\d{1,2}日 {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
                        dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                    }

                    if (ObjectUtils.isEmpty(dateFormat)) {
                        setValue(null);
                        return;
                    }

                    Date parse = dateFormat.parse(dateStr);
                    setValue(parse);

                } catch (Exception ex) {
                    setValue(null);
                }
            }
        });
    }

    @GetMapping("/init")
    public ModelAndView init() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("test16");
        return modelAndView;
    }

    @PostMapping("/receivePost")
    @ResponseBody
    public void receivePost(Test16Form form) {
        System.out.println(form);
    }
}

4.4 效果

  • 可以看到 yyyy-MM-dd HH:mm:ss 和 yyyy年MM月dd日 HH:mm:ss 格式的字符串日期都被转换为Date数类型
  • 因为没有指定转换特定的属性名所对应的数据,所以包括一览中的数据也被成功转换
  • 一览中的字符换的前后空白也被清除,一览中的日期格式的也被成功转换
    在这里插入图片描述

五. 其他自定义属性编辑器实例

5.1 自定义SexPropertyEditor

  • 对性别进行编辑,如果性别为空或者不为男性或者女性,默认设置为男性
import org.springframework.util.ObjectUtils;

import java.beans.PropertyEditorSupport;
import java.util.Arrays;
import java.util.List;

public class SexPropertyEditor extends PropertyEditorSupport {

    private final static List<String> sexList = Arrays.asList("男", "女");

    @Override
    public void setAsText(String sex) {

        // 当性别为空或者不是男或女的时候,默认设置为男性
        if(ObjectUtils.isEmpty(sex) || !sexList.contains(sex)) {
            setValue("男");
            return;
        }

        setValue(sex);
    }
}

5.2 自定义StringToListPropertyEditor

  • 将参数中的属性名=XXX-XXX-XXX的数据转换为数组
import org.springframework.util.ObjectUtils;

import java.beans.PropertyEditorSupport;

public class StringToListPropertyEditor extends PropertyEditorSupport {

    @Override
    public void setAsText(String text){

        if (ObjectUtils.isEmpty(text) || !text.contains("-")) {
            setValue(text);
            return;
        }
        setValue(text.split("-"));
    }
}

5.3 form实体类

  • Test16Form01.java
import lombok.Data;

@Data
public class Test16Form01 {

    private String sex;

    private String[] numList;

    private String[] addList;
}

5.4 前端

const url = `/test16/receiveNumListAndSex?sex=不明&numList=1-2-3&addList=4-5-6`;
$.ajax({
    url,
    type: 'GET',
    success: function (data, status, xhr) {
        console.log(data);
    }
});

5.5 Controller层

@Controller
@RequestMapping("/test16")
public class Test16Controller {

    @InitBinder
    public void formBinder(WebDataBinder binder) {

        // 当数据类型为String[],且 属性名为 numList 的时候才会起作用
        // 虽然addList也是String[]格式的数据,但是我们并没有指定转换此属性
        binder.registerCustomEditor(String[].class, "numList", new StringToListPropertyEditor());
        
        // 当数据类型为String 且 属性名为 sex 的时候才会起作用
        binder.registerCustomEditor(String.class, "sex", new SexPropertyEditor());
    }
    
    @GetMapping("/receiveNumListAndSex")
    @ResponseBody
    public void receiveNumList(Test16Form01 form) {
        System.out.println(form);
    }
}

5.6 效果

在这里插入图片描述

六. 多个@InitBinder注解修饰的方法

  • 如果@InitBinder注解没有添加value值,则每个请求都会走被其修饰的方法
  • 如果@InitBinder注解有value值,则只有参数的名称与其相同才会走此方法
import com.example.jmw.common.bindEditor.SexPropertyEditor;
import com.example.jmw.common.bindEditor.StringToListPropertyEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/test16")
public class Test16Controller {

    // 注解没有添加value值,每个请求都会走此方法
    @InitBinder
    public void init(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }

    // 指定只有参数名称为test16Form01的,才会走此方法
    @InitBinder("test16Form01")
    public void formBinder(WebDataBinder binder) {
        // 当数据类型为String 且 属性名为 sex 的时候才会起作用
        binder.registerCustomEditor(String.class, "sex", new SexPropertyEditor());
    }

    // 指定只有参数名称为test16Form的,才会走此方法
    @InitBinder("test16Form")
    public void receiveGetBinder(WebDataBinder binder) {
        // 当数据类型为String[],且 属性名为 numList 的时候才会起作用
        binder.registerCustomEditor(String[].class, "numList", new StringToListPropertyEditor());
    }

    @GetMapping("/receiveGet")
    @ResponseBody
    public void receiveGet(Test16Form form) {

        System.out.println(form);
    }

    @GetMapping("/receiveNumListAndSex")
    @ResponseBody
    public void receiveNumList(Test16Form01 form) {
        System.out.println(form);
    }
}

在这里插入图片描述

七. 其他用法

  1. 当前Controller继承父类,在父类中使用@InitBinder注解来修饰的方法
  2. 配合@ControllerAdvice注解作用于全局,具体用法请参考 顶部的参考资料

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/61239.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

华为机试 - 任务最优调度

目录 题目描述 输入描述 输出描述 用例 题目解析 算法源码 题目描述 给定一个正整数数组表示待系统执行的任务列表&#xff0c;数组的每一个元素代表一个任务&#xff0c;元素的值表示该任务的类型。 请计算执行完所有任务所需的最短时间。 任务执行规则如下: 任务可…

Springboot RabbitMq源码解析之RabbitListener注解 (四)

文章目录1.RabbitListener注解介绍2.EnableRabbit和RabbitBootstrapConfiguration3.RabbitListenerAnnotationBeanPostProcessor4.对RabbitListener注解的解析5.RabbitListenerEndpointRegistrar1.RabbitListener注解介绍 RabbitListener是Springboot RabbitMq中经常用到的一个…

D-023 DVI硬件电路设计

DVI硬件电路设计1 简介1.1 连接器1.2 接口信号定义1.3 DVI的分类1.4 DVI规格2 硬件设计实战3 硬件设计要点3.1 注意事项3.2 补充说明3.3 VGA 和 DVI 优缺点1 简介 DVI(Digital Visual Interface)是一种数字视频接口&#xff0c;它是基于TMDS (Transition Minimized Differenti…

MFC列表控件的用法(基于对话框的编程)

目录 一、List Control列表控件属性 1.List Control 2.View属性 二、OnInitDialog初始化列表 1.创建List Control的变量 2.找OnInitDialog ​3. InsertColumn插入表头 4. InsertColumn设置对齐方式和列宽 5. 设置List的正文内容 ​6.循环结构创建列表 7.设置列表整行…

Windows内核--子系统(3.5)

到底什么是子系统? 子系统是用户层概念。在Windows内核之上&#xff0c;如果想要执行类UNIX应用程序&#xff0c;就是POSIX子系统&#xff0c;如果要类似OS/2环境&#xff0c;就是OS/2子系统。 如何能模拟出不同子系统呢? 一般需要子系统用户态应用程序和相关DLL支援。 对于W…

腾讯云服务器mysql安装

1.选择mysql版本 2.安装mysql源 sudo wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm 3.下载mysql.rpm源 wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm 4.安装下载好的rpm包 sudo rpm -ivh mysql80-community-rele…

PCB入门介绍与电阻电容电感类元件的创建

摘自凡亿教育 目录 一、PCB入门介绍 二、电阻电容电感类元件的创建 1.绘制电阻的原理图库 2.绘制电容的原理图库 3.绘制电感的原理图 一、PCB入门介绍 1.EDA工具 Cadence Allegro :IC-芯片设计 Mentor PADS:做消费类电子产品、手机、机顶盒、平板电脑 Altium Designer…

多线程初阶(二)

目录 前言&#xff1a; synchronized 解析 可重入和不可重入问题 解析 Java中线程安全类 死锁问题 解析 解决死锁问题 解析 内存可见性 解析 volatile关键字 解析 wait&#xff0c;notify 解析 小结&#xff1a; 前言&#xff1a; 针对上篇文章讲到的线程安全…

VSCode\\VS2017下CPP环境的配置

VSCode下C环境配置一些问题VSCode下配置C环境&#xff1a;VSCode与boost总结&#xff1a;坑位待填&#xff1a;VSCode中3个json文件的作用&#xff1a;环境配置出现的问题以及解决VS2017 配置 C环境VS配置boost库包含项目中的自定义的.hpp文件&#xff0c;.h文件VSCode下配置C环…

公众号网课题库接口

公众号网课题库接口 本平台优点&#xff1a; 多题库查题、独立后台、响应速度快、全网平台可查、功能最全&#xff01; 1.想要给自己的公众号获得查题接口&#xff0c;只需要两步&#xff01; 2.题库&#xff1a; 题库&#xff1a;题库后台&#xff08;点击跳转&#xff09;…

4.验证面试高频问题整理(附答案)

目录 Q76.package如何使用 Q77.如何在子类中调用父类中的方法 Q78.bit[7:0]和byte有什么区别 Q79.类中的方法和类外的方法有什么区别 Q80.如何将类中的方法定义在类外 Q81.modport的用途是什么 Q82.struct和union的异同 Q83.$rose和posedge区别 Q84.如何在fork...join结…

[附源码]Python计算机毕业设计Django人事管理系统

项目运行 环境配置&#xff1a; Pychram社区版 python3.7.7 Mysql5.7 HBuilderXlist pipNavicat11Djangonodejs。 项目技术&#xff1a; django python Vue 等等组成&#xff0c;B/S模式 pychram管理等等。 环境需要 1.运行环境&#xff1a;最好是python3.7.7&#xff0c;…

js对象易混淆知识

js对象易混淆知识 __proto__ vs prototype __proto__和constructor属性是对象所独有的。 __proto__属性的作用就是当访问一个对象的属性时&#xff0c;如果该对象内部不存在这个属性&#xff0c;那么就会去它的__proto__属性所指向的那个对象&#xff08;父对象&#xff09;…

三菱FX5U PLSV指令-可变速度输出

三菱FX5U PLSV指令-可变速度输出,程序如下 该指令用于输出带旋转方向输出的变速脉冲。只支持CPU模块 *1 只能使用Y。 *2 输出模式为CW/CCW时&#xff0c;请指定CCW轴。使用Y时&#xff0c;只能指定本轴的SIGN输出或通用输出。 *3 不能使用T、ST、C 以上是指定FX3操作数得情况…

JVM总结全

虚拟机 HotSpot 默认虚拟机 JRockit HotSpot融合了JRockit jdk8初步融合完成 没有解释器&#xff0c;只有编译器 IBM J9 结构图 类加载子系统Q 1.类加载器 ​ 启动类加载器&#xff08;引导类加载器&#xff09;Bootstrap ClassLoader ​ 加载java 核心类库&#xff0c;…

QT + FFmpeg 5.x + x264 + x265 + SDL2 音视频播放器

QT FFmpeg 5.x x264 x265 SDL2 音视频播放器 使用了QT的QML设计界面,人机交互; 使用了FFmpeg 5.x x264 x265 SDL2 完成了音视频的解析到播放; 阅读了ffplay的源码,用到了ffplay的核心思想. 想熟悉ffmpeg和ffplay的朋友,都可以参考学习. 代码自取: https://github.c…

秒杀实现技巧

需求分析 “秒杀”这个词在电商行业中出现的频率较高&#xff0c;如京东或者淘宝平台的各种“秒杀”活动&#xff0c;最典型的就是“双11抢购”。 “秒杀”是指在有限的时间内对有限的商品数量进行抢购的一种行为&#xff0c;这是商家以“低价量少”的商品来获取用户的一种营…

Golang原理分析:切片(slice)原理及扩容机制

《Go语言精进之路》切片相关章节学习笔记及实验。 1.切片原理 说切片之前&#xff0c;先看看Go语言数组。Go数组是一个固定长度的、容纳同构类型元素的连续序列&#xff0c;因此Go数组类型具有两个属性&#xff1a;长度及类型&#xff1a; var a [1]int var b [2]byte var c …

【Web安全】文件上传漏洞

目录 1. 文件上传漏洞概述 1.1 FCKEditor文件上传漏洞 1.2 绕过文件上传检查功能 2. 功能还是漏洞 2.1 Apache文件解析 2.2 IIS文件解析 2.3 PHP CGI路径解析 2.4 利用上传文件钓鱼 3. 设计安全的文件上传功能 1. 文件上传漏洞概述 文件上传漏洞是指用户上传了一个…

R语言学习笔记——入门篇:第四章-基本数据管理

# R语言R语言学习笔记——入门篇&#xff1a;第四章-基本数据管理 文章目录一、示例二、创建新变量三、变量的重编码四、变量的重命名4.1、交互式编辑器4.2、函数编程五、缺失值5.1、重编码某些值为缺失值5.2、在分析中排除缺失值六、日期值6.1、将日期值转换回字符型变量6.2、…