Spring注解:InitBinder
注解 InitBinder 是用来初始化绑定器Binder的,而Binder是用来绑定数据的,换句话说就是将请求参数转成数据对象。
@InitBinder用于在@Controller中标注于方法,表示为当前控制器注册一个属性编辑器或者其他,只对当前的Controller有效。
@InitBinder 有2个基本用途,类型转换和参数绑定。
类型转换
比如,将“2019-12-06 16:59:59”这样的字符串转成 java.util.Date 对象
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
参数绑定
比如,html表单是下面这样的
<form action="/buy" method="post">
name: <input type="text" name="customer.name"> <br>
age: <input type="text" name="customer.customerId"> <br>
name: <input type="text" name="goods.title"> <br>
age: <input type="text" name="goods.price"> <br>
<input type="submit">
</form>
在后台将以customer为前缀的参数绑定到Customer对象上,将以goods为前缀的参数绑定到Goods对象上
@InitBinder("customer")
public void initCustomer(WebDataBinder binder) {
binder.setFieldDefaultPrefix("customer.");
}
@InitBinder("goods")
public void initGoods(WebDataBinder binder) {
binder.setFieldDefaultPrefix("goods.");
}
@PostMapping("/buy")
public ModelAndView buy(Customer customer, @ModelAttribute("goods") Goods goods, ModelAndView mv) {
// do something
return mv;
}
@ModelAttribute("goods") 中的 “goods” 用来指定 @InitBinder("goods")
换句话讲
在 initGoods 方法中,将以 goods 为前缀的参数封装为名为 goods 的对象;
在 buy 方法中使用 @ModelAttribute("goods") 来接收名为 goods 的对象。
Spring注解:InitBinder的更多相关文章
- spring注解说明之Spring2.5 注解介绍(3.0通用)
spring注解说明之Spring2.5 注解介绍(3.0通用) 注册注解处理器 方式一:bean <bean class="org.springframework.beans.fac ...
- springMVC注解@initbinder日期类型的属性自动转换
在实际操作中经常会碰到表单中的日期 字符串和Javabean中的日期类型的属性自动转换, 而springMVC默认不支持这个格式的转换,所以必须要手动配置, 自定义数据类型的绑定才能实现这个功能. 一 ...
- SpringMVC注解@initbinder解决类型转换问题
在使用SpringMVC的时候,经常会遇到表单中的日期字符串和JavaBean的Date类型的转换,而SpringMVC默认不支持这个格式的转换,所以需要手动配置,自定义数据的绑定才能解决这个问题.在 ...
- springMVC注解@initbinder
在实际操作中经常会碰到表单中的日期 字符串和Javabean中的日期类型的属性自动转换, 而springMVC默认不支持这个格式的转换,所以必须要手动配置, 自定义数据类型的绑定才能实现这个功能. 比 ...
- spring注解源码分析--how does autowired works?
1. 背景 注解可以减少代码的开发量,spring提供了丰富的注解功能.我们可能会被问到,spring的注解到底是什么触发的呢?今天以spring最常使用的一个注解autowired来跟踪代码,进行d ...
- Spring注解
AccountController .java Java代码 1. /** 2. * 2010-1-23 3. */ 4. packag ...
- spring 注解的优点缺点
注解与XML配置的区别 注解:是一种分散式的元数据,与源代码耦合. xml :是一种集中式的元数据,与源代码解耦. 因此注解和XML的选择上可以从两个角度来看:分散还是集中,源代码耦合/解耦. 注解的 ...
- 使用Spring注解来简化ssh框架的代码编写
目的:主要是通过使用Spring注解的方式来简化ssh框架的代码编写. 首先:我们浏览一下原始的applicationContext.xml文件中的部分配置. <bean id="m ...
- spring注解scheduled实现定时任务
只想说,spring注解scheduled实现定时任务使用真的非常简单. 一.配置spring.xml文件 1.在beans加入xmlns:task="http://www.springfr ...
随机推荐
- Android Animation动画详解(一): 补间动画
前言 你有没有被一些APP中惊艳的动画效果震撼过,有没有去思考,甚至研究过这些动画是如何实现的呢? 啥?你没有思考,更没有研究过? 好吧,那跟着我一起来学习下如何去实现APP中那些让我们惊羡的动画特效 ...
- ip2long之后有什么好处?
ip2long需要bigint来存储,而且在32位和64位系统中存储方式还有区别: 而保存成字符串,只需要char20即可. 那么,ip2long好处在哪? 做投票项目的时候,将ip地址处理后用int ...
- HOSt ip is not allowed to connect to this MySql server, MYSQL添加远程用户或允许远程访问三种方法
HOSt ip is not allowed to connect to this MySql server 报错:1130-host ... is not allowed to connect to ...
- Java集合系统
前言: 要想学习java的集合体系,就必须先了解java的集合框架,总的来说,分为Collection和Map体系. Collection集合框架: Map集合框架: 一. Collection接口 ...
- Educational Codeforces Round 65 (Rated for Div. 2) E. Range Deleting(思维+coding)
传送门 参考资料: [1]:https://blog.csdn.net/weixin_43262291/article/details/90271693 题意: 给你一个包含 n 个数的序列 a,并且 ...
- Js中“==”和“===”的区别
从字面上来讲,‘==’代表相等,‘===’代表严格相等. 具体来讲,比较过程如下: 比较过程: ‘==’: 1. 首先判断两个值的类型是否相同,如果相同,进行‘===’判断. 2. ...
- UVA 247"Calling Circles"(floyd求传递闭包+SCC)
传送门 题意: 如果两个人相互打电话(直接或间接),则说他们在同一个电话圈里. (a,b) 表示 a 打给 b: 例如,(a,b),(b,c),(c,d),(d,a),则这四个人在同一个电话圈里: 输 ...
- 9月29更新美版T-mobile版本iPhone7代和7P有锁机卡贴解锁方法
T版是块难解的砖头,之前一直没有找到稳定解锁办法,经过多次不写努力和实验,终于解决 不管是用超雪卡贴还是GPP卡贴,第一次先用连接WIFI激活手机! 注意:一定不要用ICCID通用激活,或者是TM ...
- H3C 端口绑定基本配置
- POJ 1511 Invitation Cards(逆向思维 SPFA)
Description In the age of television, not many people attend theater performances. Antique Comedians ...