Java Code Examples for org.springframework.web.bind.ServletRequestDataBinder

Following code examples demonstrate how to use org.springframework.web.bind.ServletRequestDataBinderfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.web.bind.ServletRequestDataBinderand various code implementation of this class.

    public void configureBinder(ApplicationContext applicationContext, List<Map> customEditors, ServletRequestDataBinder binder) throws Exception {
        for (Map config : customEditors) {
            Class editorType = Class.forName(config.get("editorType").toString());
            Class propertyType = Class.forName(config.get("propertyType").toString());
            Class collectionType = Class.forName(config.get("collectionType").toString());

            PropertyEditor editor;

            if(editorType.equals(CustomJsonCollectionEditor.class)){
                Constructor[] candidates = editorType.getDeclaredConstructors();
                Constructor contructorToUse = null;
                for(Constructor candidate : candidates){
                    Class[] classes = candidate.getParameterTypes();
                    if(classes != null && classes.length == 2){
                        contructorToUse = candidate;
                        break;
                    }

                }
                editor = (PropertyEditor) BeanUtils.instantiateClass(contructorToUse, new Object[]{ collectionType, propertyType });
            }
            else if(editorType.equals(CustomEntityIdCollectionEditor.class)){
                Constructor[] candidates = editorType.getDeclaredConstructors();
                Constructor contructorToUse = null;
                for(Constructor candidate : candidates){
                    Class[] classes = candidate.getParameterTypes();
                    if(classes != null && classes.length == 3){
                        contructorToUse = candidate;
                        break;
                    }

                }
                Object manager = applicationContext.getBean("entityManager");
                editor = (PropertyEditor) BeanUtils.instantiateClass(contructorToUse, new Object[]{ collectionType, propertyType, manager });

            }else{
                editor = (PropertyEditor) BeanUtils.instantiateClass(editorType);
            }

            if(collectionType != null){
                binder.registerCustomEditor(collectionType, config.get("property").toString(), editor);
            }
            else{
                binder.registerCustomEditor(propertyType, editor);
            }
        }
    } 


    
    public ServletRequestDataBinder create(HttpServletRequest request,
            Object command, String requestName) {
        ServletRequestDataBinder binder = new ServletRequestDataBinder(command,
                requestName);
        binder.registerCustomEditor(byte[].class,
                new ByteArrayMultipartFileEditor());
        return binder;
    } 

Advertisement
Javadoc
Special org.springframework.validation.DataBinder to perform data binding from servlet request param

eters to JavaBeans, including support for multipart files.

See the DataBinder/WebDataBinder superclasses for customization options, which include specifying allowed/required fields, and registering custom property editors.

Can also be used for manual data binding in custom web controllers: for example, in a plain Controller implementation or in a MultiActionController handler method. Simply instantiate a ServletRequestDataBinder for each binding process, and invoke bind with the current ServletRequest as argument: MyBean myBean = new MyBean(); // apply binder to custom target object ServletRequestDataBinder binder = new ServletRequestDataBinder(myBean); // register custom editors, if desired binder.registerCustomEditor(...); // trigger actual binding of request parameters binder.bind(request); // optionally evaluate binding errors Errors errors = binder.getErrors(); ... @author Rod Johnson @author Juergen Hoeller @see #bind(javax.servlet.ServletRequest) @see #registerCustomEditor @see #setAllowedFields @see #setRequiredFields @see #setFieldMarkerPrefix

Read More
Advertisement