数据绑定二

2023-07-16  本文已影响0人  程序员札记

基本流程图

image.png

bindRequestParameters绑定请求参数

如果不是禁止绑定的话,就要进行请求参数的绑定,禁止绑定就是ModelAttribute注解里的binding属性。

image.png

bindRequestParameters绑定请求参数

我们直接深入里面的核心方法,其他的以前讲过。


image.png

doBind准备绑定

这里参数也准备好了,就是请求参数,包括了表单和uri的。

image.png
    @Override
    protected void doBind(MutablePropertyValues mpvs) {
        checkFieldDefaults(mpvs);
        checkFieldMarkers(mpvs);
        super.doBind(mpvs);
    }

checkFieldDefaults先进行属性前缀删除

这里就是会遍历请求参数,然后获取到对应的前缀属性,把前缀删除,重新放入参数集合里,把顺便把原来的属性删除,里面涉及的比较深,我们可以稍微跟一下看看。

    protected void checkFieldDefaults(MutablePropertyValues mpvs) {
        String fieldDefaultPrefix = getFieldDefaultPrefix();
        if (fieldDefaultPrefix != null) {
            PropertyValue[] pvArray = mpvs.getPropertyValues();
            for (PropertyValue pv : pvArray) {//遍历参数,如果不是前缀的话就删除
                if (pv.getName().startsWith(fieldDefaultPrefix)) {
                    String field = pv.getName().substring(fieldDefaultPrefix.length());//取出前缀后的内容
                    if (getPropertyAccessor().isWritableProperty(field) && !mpvs.contains(field)) {//是否可写,且不包含在属性集合里
                        mpvs.add(field, pv.getValue());//添加到属性集合
                    }
                    mpvs.removePropertyValue(pv);//处理完删除原来的
                }
            }
        }
    }

getPropertyAccessor获取属性访问器

getInternalBindingResult获取绑定结果

可以看到层层深入,最后创建了BeanPropertyBindingResult 绑定结果。

    protected ConfigurablePropertyAccessor getPropertyAccessor() {
        return getInternalBindingResult().getPropertyAccessor();
    }

    protected AbstractPropertyBindingResult getInternalBindingResult() {
        if (this.bindingResult == null) {
            initBeanPropertyAccess();
        }
        return this.bindingResult;
    }

    public void initBeanPropertyAccess() {
        Assert.state(this.bindingResult == null,
                "DataBinder is already initialized - call initBeanPropertyAccess before other configuration methods");
        this.bindingResult = createBeanPropertyBindingResult();
    }
    //创建绑定结果,设置转换器和解码起
    protected AbstractPropertyBindingResult createBeanPropertyBindingResult() {
        BeanPropertyBindingResult result = new BeanPropertyBindingResult(getTarget(),
                getObjectName(), isAutoGrowNestedPaths(), getAutoGrowCollectionLimit());

        if (this.conversionService != null) {
            result.initConversion(this.conversionService);//第一次会创建beanWrapper
        }
        if (this.messageCodesResolver != null) {
            result.setMessageCodesResolver(this.messageCodesResolver);
        }

        return result;
    }

绑定结果对象:


image.png

getPropertyAccessor属性处理器

这个在前面result.initConversion(this.conversionService);的时候就创建了,其实就是做了包装,加了一些其他的属性。

    @Override
    public final ConfigurablePropertyAccessor getPropertyAccessor() {
        if (this.beanWrapper == null) {
            this.beanWrapper = createBeanWrapper();
            this.beanWrapper.setExtractOldValueForEditor(true);
            this.beanWrapper.setAutoGrowNestedPaths(this.autoGrowNestedPaths);
            this.beanWrapper.setAutoGrowCollectionLimit(this.autoGrowCollectionLimit);
        }
        return this.beanWrapper;
    }

isWritableProperty是否是可写属性

这个比较深,但是内部有个比较重要的地方要说下:


image.png
getCachedIntrospectionResults
image.png

里面就是获取属性的地方,这里决定你的属性名是否可以对应。


image.png image.png
image.png
image.png image.png
image.png

内部属性设置是根据方法名来的:


image.png
上一篇 下一篇

猜你喜欢

热点阅读