@InitBinder的作用
- 由@InitBinder表示的方法,可以对WebDataBinder对象进行初始化。WebDataBinder是DataBinder的子类,用于完成由表单到JavaBean属性的绑定。
- @InitBinder方法不能有返回值,它必须盛名为void。
- @InitBinder方法的参数通常是WebDataBinder,@InitBinder可以对WebDataBinder进行初始化
在使用SpringMVC的时候,经常会遇到表单中的日期字符串和JavaBean的Date类型的转换,而SpringMVC默认不支持这个格式的转换,所以需要手动配置,自定义数据的绑定才能解决这个问题。
在需要日期转换的Controller中使用SpringMVC的注解@initbinder和Spring自带的WebDateBinder类来操作。
WebDataBinder是用来绑定请求参数到指定的属性编辑器.由于前台传到controller里的值是String类型的,当往Model里Set这个值的时候,如果set的这个属性是个对象,Spring就会去找到对应的editor进行转换,然后再SET进去。
@InitBinder
public void initBinder(WebDataBinder binder){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 严格限制日期转换
sdf.setLenient(false);
//true:允许输入空值,false:不能为空值
binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
}
initBinder方法用在controller类中
spring mvc在绑定表单之前,都会先注册这些编辑器,Spring自己提供了大量的实现类,诸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等许多,基本上够用。
使用时候调用WebDataBinder的registerCustomEditor方法
registerCustomEditor源码:
public void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor) {
getPropertyEditorRegistry().registerCustomEditor(requiredType, propertyEditor);
}
第一个参数requiredType是需要转化的类型。
第二个参数PropertyEditor是属性编辑器,它是个接口,以上提到的如CustomDateEditor等都是继承了实现了这个接口的PropertyEditorSupport类。
@InitBinder的作用的更多相关文章
- SpringMVC使用@PathVariable,@RequestBody,@ResponseBody,@RequestParam,@InitBinder
@Pathvariable public ResponseEntity<String> ordersBack( @PathVariable String reqKey, ...
- @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") 前台request 获取body的格式是正确的 (2018-03-23 16:44:22) 但是Java 后台却格式化成了yyyy-MM-dd的格式 巨坑(@InitBinder搞得贵)
最近做项目时,同事写的功能总是格式化时间不正确,Java类属性明明注解了@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") 但就是硬 ...
- 从原理层面掌握@InitBinder的使用【享学Spring MVC】
每篇一句 大魔王张怡宁:女儿,这堆金牌你拿去玩吧,但我的银牌不能给你玩.你要想玩银牌就去找你王浩叔叔吧,他那银牌多 前言 为了讲述好Spring MVC最为复杂的数据绑定这块,我前面可谓是做足了功课, ...
- @RestControllerAdvice作用及原理
原文:Spring Boot 系列(八)@ControllerAdvice 拦截异常并统一处理 在spring 3.2中,新增了@ControllerAdvice 注解,可以用于定义@Exceptio ...
- SpringBoot#InitBinder
__震惊!!我的天啦,OMG!! 1. initBinder对我而言的价值在于,通过传统表单post数据到后端的controller时候,数据类型的自动转换.比如前端页面填写一个日期字符串,通过Ini ...
- 注解式项目开发!详细解析Java中各个注解的作用和使用方式
@Target 作用: 指明了修饰的这个注解的使用范围, 即被描述的注解可以用在哪里 @Target(ElementType.Type) ElementType取值的类型: TYPE: 类,接口或者枚 ...
- @ModelAttribute 与@InitBinder
3.4.6 @ModelAttribute 注解 Mod lAttribut 通常作用在 Controller 的某个方法上,此方法会首先被调用, 井将方法 结果作为 Model 的属性 然后再调用对 ...
- if __name__== "__main__" 的意思(作用)python代码复用
if __name__== "__main__" 的意思(作用)python代码复用 转自:大步's Blog http://www.dabu.info/if-__-name__ ...
- (转载)linux下各个文件夹的作用
linux下的文件结构,看看每个文件夹都是干吗用的/bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基 ...
随机推荐
- git获取特定版本的代码
比如取1.80版本 git clone -b https://github.com/passedbylove/poiji.git
- wmic查询ip
@echo off for /F "usebackq" %%R in (`wmic PATH Win32_NetworkAdapterConfiguration WHERE &qu ...
- ListView中用鼠标拖动各项上下移动的问题。(100分)
在OnDragDrop事件中處理:以下是delphi的例子 procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integ ...
- AESTest
using Gaea.MySql; using System; using System.Data; using System.IO; using System.Security.Cryptograp ...
- python爬虫概述
爬虫的使用:爬虫用来对网络的数据信息进行爬取,通过URL的形式,将数据保存在数据库中并以文档形式或者报表形式进行展示. 爬虫可分为通用式爬虫或特定式爬虫,像我们经常用到的搜索引擎就属于通用式爬虫,如果 ...
- 【AMADM】django-braces -- Django的一些可重用的,通用型的mixin
动机 简介 个人评分 动机 Django1.8以后加入了Class Based View,以及GenericView概念.之后在django对类的使用更加频繁. 而一些特性,相信我们都有重复地编写过. ...
- Pytorch1.0入门实战三:ResNet实现cifar-10分类,利用visdom可视化训练过程
人的理想志向往往和他的能力成正比. —— 约翰逊 最近一直在使用pytorch深度学习框架,很想用pytorch搞点事情出来,但是框架中一些基本的原理得懂!本次,利用pytorch实现ResNet神经 ...
- firefox PAC代理
在linux用firefox,又不像windows下的chrome那样,可以直接设置个自动代理,但是发现firefox有个自动代理的功能哦. /home/allen/Documents/google. ...
- python 爬虫 基于requests模块的get请求
需求:爬取搜狗首页的页面数据 import requests # 1.指定url url = 'https://www.sogou.com/' # 2.发起get请求:get方法会返回请求成功的响应对 ...
- 【坑】不要使用各种框架提供的内部List
为了代码简洁有时我们会使用一些框架提供的工具类.如 import org.apache.commons.collections.ListUtils; package java.util.Collect ...