SpringBoot?@InitBinder注解綁定請求參數(shù)的過程詳解
一. 作用
作用于Controller層中,在Controller層的方法執(zhí)行前執(zhí)行,主要作用是初始化當(dāng)前Controller層的數(shù)據(jù)綁定器(或者屬性綁定器),幫助完成數(shù)據(jù)處理和數(shù)據(jù)綁定。
被該注解修飾的方法會有一個形參WebDataBinder,可以幫我們將request請求中的參數(shù)處理綁定到JavaBean中。
二. 前期準(zhǔn)備
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">發(fā)送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層
StringTrimmerEditor和CustomDateEditor是框架自帶的屬性處理器
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)); // 只有當(dāng)屬性名為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格式的日期被轉(zhuǎn)換為Date格式的日期
四. Post請求 + 表單傳值 + 自定義日期屬性綁定器
4.1 前臺-test16.html
表單提交的數(shù)據(jù)若包含List<實體類>這種數(shù)據(jù)結(jié)構(gòu)
在前臺需要用 form對應(yīng)的屬性名[下標(biāo)].實體類屬性名
這種方式準(zhǔn)備數(shù)據(jù)
<!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">發(fā)送post請求</button> </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實體類區(qū)域 "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', // 后端返回給前端的數(shù)據(jù)類型 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; // 待轉(zhuǎn)換類型 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; // 待轉(zhuǎn)換類型 private Date workDate; }
4.3 Controller層
我們可以通過PropertyEditorSupport類來實現(xiàn)我們自己的屬性編輯器
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)); // 自定義日期轉(zhuǎn)換屬性處理器 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 格式的字符串日期都被轉(zhuǎn)換為Date數(shù)類型
- 因為沒有指定轉(zhuǎn)換特定的屬性名所對應(yīng)的數(shù)據(jù),所以包括一覽中的數(shù)據(jù)也被成功轉(zhuǎn)換
- 一覽中的字符換的前后空白也被清除,一覽中的日期格式的也被成功轉(zhuǎn)換
五. 其他自定義屬性編輯器實例
5.1 自定義SexPropertyEditor
對性別進(jìn)行編輯,如果性別為空或者不為男性或者女性,默認(rèn)設(shè)置為男性
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) { // 當(dāng)性別為空或者不是男或女的時候,默認(rèn)設(shè)置為男性 if(ObjectUtils.isEmpty(sex) || !sexList.contains(sex)) { setValue("男"); return; } setValue(sex); } }
5.2 自定義StringToListPropertyEditor
將參數(shù)中的屬性名=XXX-XXX-XXX的數(shù)據(jù)轉(zhuǎn)換為數(shù)組
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) { // 當(dāng)數(shù)據(jù)類型為String[],且 屬性名為 numList 的時候才會起作用 // 雖然addList也是String[]格式的數(shù)據(jù),但是我們并沒有指定轉(zhuǎn)換此屬性 binder.registerCustomEditor(String[].class, "numList", new StringToListPropertyEditor()); // 當(dāng)數(shù)據(jù)類型為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值,則只有參數(shù)的名稱與其相同才會走此方法
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)); } // 指定只有參數(shù)名稱為test16Form01的,才會走此方法 @InitBinder("test16Form01") public void formBinder(WebDataBinder binder) { // 當(dāng)數(shù)據(jù)類型為String 且 屬性名為 sex 的時候才會起作用 binder.registerCustomEditor(String.class, "sex", new SexPropertyEditor()); } // 指定只有參數(shù)名稱為test16Form的,才會走此方法 @InitBinder("test16Form") public void receiveGetBinder(WebDataBinder binder) { // 當(dāng)數(shù)據(jù)類型為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); } }
七. 其他用法
- 當(dāng)前Controller繼承父類,在父類中使用@InitBinder注解來修飾的方法
- 配合@ControllerAdvice注解作用于全局,具體用法請參考下面的參考資料
參考資料:
到此這篇關(guān)于SpringBoot @InitBinder注解綁定請求參數(shù)的文章就介紹到這了,更多相關(guān)SpringBoot @InitBinder注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot利用Redis實現(xiàn)接口冪等性攔截
這篇文章主要為大家介紹了Springboot如何利用Redis實現(xiàn)接口冪等性攔截。本文將通過自定義注解+redis+攔截器+MD5?實現(xiàn),感興趣的可以了解一下2022-06-06使用idea遠(yuǎn)程調(diào)試jar包的配置過程
這篇文章主要介紹了使用idea遠(yuǎn)程調(diào)試jar包的配置過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09關(guān)于SpringBoot中Ajax跨域以及Cookie無法獲取丟失問題
這篇文章主要介紹了關(guān)于SpringBoot中Ajax跨域以及Cookie無法獲取丟失問題,本文具有參考意義,遇到相同或者類似問題的小伙伴希望可以從中找到靈感2023-03-03