当前位置:   article > 正文

SpringMVC中的@InitBinder注解【记录】

initbinder注解

一、Spring请求参数绑定流程:

1、请求参数绑定流程:

我们在开发的时候,经常会从html,jsp中将请求参数通过request对象传递到后台,可是经常会遇到这么一种情况,那就是传过来的数据到后台后,还要再组装成一种对象的格式。
在这里插入图片描述
2、Spring中请求参数绑定:

Spring可以自动将request中的请求参数数据绑定到对象的每个property上,但是只会绑定一些简单数据类型(比如Strings,int,float)到对应的对象中。可是如果面对复杂的对象,那就要借助PropertyEditor接口来帮助我们完成复杂对象的绑定。

PropertyEditor这个接口提供了两个方法,一个方法是将String类型的值转成property对应的数据类型,另一个方法是将property转成String。
在这里插入图片描述
3、CustomDateEditor继承关系:
在这里插入图片描述
4、示例代码:

@InitBinder
public void InitBinder(WebDataBinder binder) {
    //前端传入的时间格式必须是"yyyy-MM-dd"效果!
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    CustomDateEditor dateEditor = new CustomDateEditor(df, true);
    binder.registerCustomEditor(Date.class, dateEditor);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

二、@InitBinder详解:

1、@InitBinder注解简介:

@InitBinder作用于@Controller中的方法,表示为当前控制器注册一个属性编辑器,对WebDataBinder进行初始化,且只对当前的Controller有效,一般用做BaseController对数据进行预处理操作。

2、@InitBinder执行时机:

@InitBinder注解被解析的时机,是其所标注的方法,在该方法被请求执行之前。同时@InitBinder标注的方法是可以多次执行的,也就是说来一次请求就执行一次@InitBinder解析。

3、@InitBinder执行原理:

当某个Controller上的第一次请求,由于SptingMVC前端控制器匹配到该Controller之后,根据Controller的class类型来查找所有标注了@InitBinder注解的方法,并且存入RequestMappingHandlerAdapter里的initBinderCache缓存中。等下一次请求执行对应业务方法之前,会先走initBinderCache缓存,而不再去解析@InitBinder。

4、@InitBinder的使用:

@InitBinder注解的方法可以对WebDataBinder初始化;WebDataBinder是用于表单到方法的数据绑定的,WebDataBinder中有很多方法可以对数据绑定进行具体的设置:

1)比如我们设置name属性为非绑定属性(也可以设置绑定值setAllowedFields):

@InitBinder
public void initBinder(WebDataBinder binder) {
	binder.setDisallowedFields("name");
  • 1
  • 2
  • 3

该字段在表单提交时就不会提交上去。

2)对页面数据进行解析绑定:

以时间为例,在Controller中接收的是Date类型,而请求的参数为String类型时,如果没有加@InitBinder的效果:

@Controller
public class MyController {
    @RequestMapping("baseTest")
    @ResponseBody
    public String baseTest(Date date){
        return String.valueOf(date);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

页面结果:由于语法格式有误,服务器无法理解此请求
在这里插入图片描述
有两种实现方式:

  • 使用Spring提供的实现类CustomDateEditor
public class BaseController {
    @InitBinder
    protected void initBinder(WebDataBinder binder){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat, true));
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 自定义实现类MyDateEditor
public class BaseController {

    @InitBinder
    protected void initBinder(WebDataBinder binder){
        binder.registerCustomEditor(Date.class,new MyDateEditor());
    }

    private class MyDateEditor extends PropertyEditorSupport{
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = null;
            try {
                date = format.parse(text);
            } catch (ParseException e) {
                format = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    date = format.parse(text);
                } catch (ParseException e1) {
                }
            }
            setValue(date);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

运行结果:
在这里插入图片描述
更多例子可以参考:springMVC之@InitBinder的用法

参考文章:SpringMVC中@InitBinder注解介绍与使用
参考文章:SpringBoot2教程29整合SpringMVC之@InitBinder处理请求参数的绑定

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/66290
推荐阅读
相关标签
  

闽ICP备14008679号