Spring的控制器注解@InitBinder

由于看到这么Mastering-Spring-5.0的一块代码,想写写@InitBinder注解:git

这里咱们处理一些date的格式,由于若是set的这个属性是个对象,Spring就会去找到对应的editor进行转换github

WebDataBinder实现将请求request绑定到复杂属性时的请求字符string到属性的转换 浏览器

而后写个方法来试试效果,这里使用一个入参date,须要的是dd/MM/yyyy的格式.net

而后咱们在浏览器中输入http://localhost:8080/ctest?date=2018/02/03,日志输出:3d

这时候咱们若是输入http://localhost:8080/ctest?date=2018-02-03,那么就会有以下的报错日志

错误是能够处理的,那么咱们再注册一个CustomDateEditor处理dd-MM-yyyy的格式对象

固然若是你有更加复杂的逻辑,那么咱们能够本身定义一个类来处理,这里须要继承PropertyEditorSupportblog

这时候咱们就能够传这样的参数http://localhost:8080/ctest?date=201802--03继承

参考:get

git@github.com:PacktPublishing/Mastering-Spring-5.0.git

https://blog.csdn.net/qq_40594137/article/details/79244690

相关文章
相关标签/搜索