SpringBoot @InitBinder注解绑定请求参数的方法是什么

发布时间:2023-04-17 15:53:44 作者:iii
来源:亿速云 阅读:127

这篇文章主要讲解了“SpringBoot @InitBinder注解绑定请求参数的方法是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringBoot @InitBinder注解绑定请求参数的方法是什么”吧!

一. 作用

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

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

SpringBoot @InitBinder注解绑定请求参数的方法是什么

二. 前期准备

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 效果

SpringBoot @InitBinder注解绑定请求参数的方法是什么

四. 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 效果

SpringBoot @InitBinder注解绑定请求参数的方法是什么

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

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 效果

SpringBoot @InitBinder注解绑定请求参数的方法是什么

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

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);
    }
}

SpringBoot @InitBinder注解绑定请求参数的方法是什么

七. 其他用法

感谢各位的阅读,以上就是“SpringBoot @InitBinder注解绑定请求参数的方法是什么”的内容了,经过本文的学习后,相信大家对SpringBoot @InitBinder注解绑定请求参数的方法是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. Spring和SpringBoot及SpringCloud之间的关系是怎样的
  2. UidGenerato如何在Spring Boot项目结合使用

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

springboot @initbinder

上一篇:DDL数据库与表怎么创建及管理

下一篇:SQL注入攻击怎么掌握

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》