目录
- @InitBinder注解的作用
- InitBinder注解源码
- 使用其他格式转化器
@InitBinder注解的作用
springmvc并不是能对所有类型的参数进行绑定的,如果对日期Date类型参数进行绑定,就会报错IllegalStateException错误。
所以需要注册一些类型绑定器用于对参数进行绑定。InitBinder注解就有这个作用。
程序代码示例:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import Java.util.Date;
@RestController
@RequestMapping("/date")
public class InitBinderController {
@RequestMapping(value = "/testInitBinder", method = RequestMethod.GET)
private String testInitBinder(Date date) {
System.out.println("date = " + date);
return "RequsetInitBindDemo";
}
}
postman测试:

不能把String类型转换为Date类型报错。
此时就需要一个日期类型转换器。
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
@RequejavascriptstMapping("/date")
public class InitBinderController {
@RequestMapping(value = "/testInitBinder", method = RequestMethod.GET)
private String testInitBinder(Date date) {
System.out.println("date = " + date);
return "RequsetInitBindDemo";
}
@InitBinder
public void dateTypeBinder(WebDataBinder webDataBinder) {
//往数据绑定器中添加一个DateFormatter日期转化器。
webDataBinder.addCustomFoandroidrmatter(new DateFormatter("yyyy-mm-dd"));
}
}
postman测试:

打印结果:
date = Tue Jan 15 00:05:00 CST 2019
InitBinder注解源码
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface InitBinder {
//指定参数名,这个不知控制器方法上形参的参数名,而是请求参数名,
//可以指定多个。指定后只有这些参数需要用到该转换器。如果不指定,默认所有。
String[] value() default {};
}
注意:并且使用InitBinder 注册的绑定器只有在当前Controller中才有效,不会作用于其他Controller。
此时,就需要用到@ControllerAdvice注解定义全局绑定器。使不同controller的方法都能作用到。
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;
@ControllerAdvice
public class InitConfig {
@InitBinder
public void dateTypeBinder(WebDataBinder webDataBinder) {
//往数据绑定器中添加一个DateFormatter日期转化器。
webDataBinder.addCustomFormatter(new DateFormatter("yyyy-mm-dd"));
}
}
使用其他格式转化器
我们可以自定义格式转化器,实现Formatter接口就可。还可以添加验证器等等。
public class StringFormatter implements Formatter<String> {
private static final String PREFIX = "convertString == ";
@Override
public String parse(String text, Locale locale) throws ParseException {
//所以String类型参数都加上一个android前缀。
String result = PREFIX + text;
return result;
}
@Override
public String print(String object, Locale locale) {
return object;
http://www.devze.com }
}
添加:
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;
@ControllerAdvice
public class InitConfig {
@InitBinder
public void dateTypeBinder(WebDataBinder webDataBinder) {
//往数据绑定器中添加一个DateFormatter日期转化器。
webphpDataBinder.addCustomFormatter(new DateFormatter("yyyy-mm-dd"));
//添加一个string类型的数据绑定器,作用是加个前缀
webDataBinder.addCustomFormatter(new StringFormatter());
}
}
测试:
@RequestMapping(value = "/testInitBinder2", method = RequestMethod.GET)
private String testInitBinder2(String name) {
System.out.println("name = " + name);
return "RequsetInitBindDemo";
}

打印结果:
name = convertString == 刘亦菲
到此这篇关于SpringMVC之@InitBinder注解详解的文章就介绍到这了,更多相关@InitBinder注解内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
加载中,请稍侯......
精彩评论