網(wǎng)站首頁 編程語言 正文
說明與作用
springmvc并不是能對所有類型的參數(shù)進(jìn)行綁定的,如果對日期Date類型參數(shù)進(jìn)行綁定,就會報(bào)錯IllegalStateException錯誤。所以需要注冊一些類型綁定器用于對參數(shù)進(jìn)行綁定。InitBinder注解就有這個作用。
@Controller
public class InitBinderController {
@RequestMapping("/testInitBinder")
private String testInitBinder(Date date){
System.out.println("date = " + date);
return "RequsetInitBindDemo";
}
}
不能把String類型轉(zhuǎn)換為Date類型報(bào)錯。
此時就需要一個日期類型轉(zhuǎn)換器。
@InitBinder
public void dateTypeBinder(WebDataBinder webDataBinder){
//往數(shù)據(jù)綁定器中添加一個DateFormatter日期轉(zhuǎn)化器。
webDataBinder.addCustomFormatter(new DateFormatter("yyyy-mm-dd"));
}
日期類型轉(zhuǎn)換成功了。
InitBinder注解:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface InitBinder {
//指定參數(shù)名,這個不知控制器方法上形參的參數(shù)名,而是請求參數(shù)名,可以指定多個。指定后只有這些參數(shù)需要用到該轉(zhuǎn)換器。如果不指定,默認(rèn)所有。
String[] value() default {};
}
并且使用InitBinder 注冊的綁定器只有在當(dāng)前Controller中才有效,不會作用于其他Controller。
在其他controller中定義一個接收請求的方法。
請求失敗。
使用@ControllerAdvice定義全局綁定器
可以使用@ControllerAdvice定義全局綁定器。ControllerAdvice的使用可以看文章
@ControllerAdvice
public class InitBinderAdviseController {
@InitBinder
public void dateTypeBinder(WebDataBinder webDataBinder){
//往數(shù)據(jù)綁定器中添加一個DateFormatter日期轉(zhuǎn)化器。
webDataBinder.addCustomFormatter(new DateFormatter("yyyy-mm-dd"));
}
}
結(jié)果:
不同controller的方法都能作用到。
使用其他格式轉(zhuǎn)化器
我們可以自定義格式轉(zhuǎn)化器,實(shí)現(xiàn)Formatter接口就可。還可以添加驗(yàn)證器等等。
public class StringFormatter implements Formatter<String> {
private static final String PREFIX = "convertString == ";
@Override
public String parse(String text, Locale locale) throws ParseException {
//所以String類型參數(shù)都加上一個前綴。
String result = PREFIX + text;
return result;
}
@Override
public String print(String object, Locale locale) {
return object;
}
}
添加
測試:
@RequestMapping("/testInitBinder2")
private String testInitBinder1(String name){
System.out.println("name = " + name);
return "RequsetInitBindDemo";
}
結(jié)果:
前綴有了。
原文鏈接:https://blog.csdn.net/qq_43985303/article/details/129520718
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-07-15 C#中Timer定時器類的簡單使用_C#教程
- 2023-06-21 詳解C#中委托的概念與使用_C#教程
- 2022-09-18 IOS開發(fā)壓縮后圖片模糊問題解決_IOS
- 2022-12-13 sql索引失效的情況以及超詳細(xì)解決方法_MsSql
- 2022-06-30 Python利用shutil模塊實(shí)現(xiàn)文件夾的復(fù)制刪除與裁剪_python
- 2022-09-08 Pandas如何將Timestamp轉(zhuǎn)為datetime類型_python
- 2022-07-22 URLClassLoader加載Class時的類初始化問題
- 2022-05-13 error hawk@0.10.2: The engine “node“ is incompatib
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支