spring笔记-DataBinder

参考:
spring笔记-PropertyAccessor
spring笔记-PropertyValue

1.使用方法

    @Test
    public void testBindingWithDefaultConversionNoErrors() throws BindException {
        TestBean rod = new TestBean();
        DataBinder binder = new DataBinder(rod, "person");
        MutablePropertyValues pvs = new MutablePropertyValues();
        pvs.add("name", "Rod");
        pvs.add("jedi", "on");

        binder.bind(pvs);
        binder.close();

        assertEquals("Rod", rod.getName());
        assertTrue(rod.isJedi());
    }

2.Servlet相关

2.1 ServletRequestParameterPropertyValues

根据ServletRequest构建PropertyValues

2.2 ServletRequestDataBinder

bind方法会自动构建ServletRequestParameterPropertyValues对象,并调用doBind方法

    public void bind(ServletRequest request) {
        MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request);
        MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class);
        if (multipartRequest != null) {
            bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
        }
        addBindValues(mpvs, request);
        doBind(mpvs);
    }

2.3 WebDataBinderFactory

WebDataBinder工厂

其中InitBinderDataBinderFactory处理@InitBinder注解对DataBinder的初始化

    @InitBinder
    protected void initBinder(final HttpServletRequest request,
                              final ServletRequestDataBinder binder)
                    throws Exception {
    }

参考:
https://blog.csdn.net/hongxingxiaonan/article/details/50282001

你可能感兴趣的:(spring笔记-DataBinder)