Example usage for org.springframework.web.bind ServletRequestParameterPropertyValues ServletRequestParameterPropertyValues

List of usage examples for org.springframework.web.bind ServletRequestParameterPropertyValues ServletRequestParameterPropertyValues

Introduction

In this page you can find the example usage for org.springframework.web.bind ServletRequestParameterPropertyValues ServletRequestParameterPropertyValues.

Prototype

public ServletRequestParameterPropertyValues(ServletRequest request, @Nullable String prefix,
        @Nullable String prefixSeparator) 

Source Link

Document

Create new ServletRequestPropertyValues supplying both prefix and prefix separator.

Usage

From source file:org.synyx.hades.extensions.web.PageableArgumentResolver.java

public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) {

    if (methodParameter.getParameterType().equals(Pageable.class)) {

        assertPageableUniqueness(methodParameter);

        Pageable request = getDefaultFromAnnotationOrFallback(methodParameter);

        ServletRequest servletRequest = (ServletRequest) webRequest.getNativeRequest();

        PropertyValues propertyValues = new ServletRequestParameterPropertyValues(servletRequest,
                getPrefix(methodParameter), separator);

        DataBinder binder = new ServletRequestDataBinder(request);

        binder.initDirectFieldAccess();// w  ww .  j a v a  2s. c  o m
        binder.registerCustomEditor(Sort.class, new SortPropertyEditor("sort.dir", propertyValues));
        binder.bind(propertyValues);

        if (request.getPageNumber() > 0) {

            request = new PageRequest(request.getPageNumber() - 1, request.getPageSize(), request.getSort());
        }

        return request;
    }

    return UNRESOLVED;
}

From source file:org.zht.framework.web.bind.resolver.FormModelMethodArgumentResolver.java

/**
 * {@inheritDoc}/*from ww w. j  av a2  s.  co  m*/
 * <p>Downcast {@link org.springframework.web.bind.WebDataBinder} to {@link org.springframework.web.bind.ServletRequestDataBinder} before binding.
 *
 * @throws Exception
 * @see org.springframework.web.servlet.mvc.method.annotation.ServletRequestDataBinderFactory
 */

protected void bindRequestParameters(ModelAndViewContainer mavContainer, WebDataBinderFactory binderFactory,
        WebDataBinder binder, NativeWebRequest request, MethodParameter parameter) throws Exception {

    Map<String, Boolean> hasProcessedPrefixMap = new HashMap<String, Boolean>();

    Class<?> targetType = binder.getTarget().getClass();
    ServletRequest servletRequest = prepareServletRequest(binder.getTarget(), request, parameter);
    WebDataBinder simpleBinder = binderFactory.createBinder(request, null, null);

    if (Collection.class.isAssignableFrom(targetType)) {//bind collection or array

        Type type = parameter.getGenericParameterType();
        Class<?> componentType = Object.class;

        Collection target = (Collection) binder.getTarget();

        List targetList = new ArrayList(target);

        if (type instanceof ParameterizedType) {
            componentType = (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0];
        }

        if (parameter.getParameterType().isArray()) {
            componentType = parameter.getParameterType().getComponentType();
        }

        for (Object key : servletRequest.getParameterMap().keySet()) {
            String prefixName = getPrefixName((String) key);

            //?prefix ??
            if (hasProcessedPrefixMap.containsKey(prefixName)) {
                continue;
            } else {
                hasProcessedPrefixMap.put(prefixName, Boolean.TRUE);
            }

            if (isSimpleComponent(prefixName)) { //bind simple type
                Map<String, Object> paramValues = WebUtils.getParametersStartingWith(servletRequest,
                        prefixName);
                Matcher matcher = INDEX_PATTERN.matcher(prefixName);
                if (!matcher.matches()) { //? array=1&array=2
                    for (Object value : paramValues.values()) {
                        targetList.add(simpleBinder.convertIfNecessary(value, componentType));
                    }
                } else { //? array[0]=1&array[1]=2
                    int index = Integer.valueOf(matcher.group(1));

                    if (targetList.size() <= index) {
                        growCollectionIfNecessary(targetList, index);
                    }
                    targetList.set(index, simpleBinder.convertIfNecessary(paramValues.values(), componentType));
                }
            } else { //? votes[1].title=votes[1].title&votes[0].title=votes[0].title&votes[0].id=0&votes[1].id=1
                Object component = null;
                //? ?????
                Matcher matcher = INDEX_PATTERN.matcher(prefixName);
                if (!matcher.matches()) {
                    throw new IllegalArgumentException("bind collection error, need integer index, key:" + key);
                }
                int index = Integer.valueOf(matcher.group(1));
                if (targetList.size() <= index) {
                    growCollectionIfNecessary(targetList, index);
                }
                Iterator iterator = targetList.iterator();
                for (int i = 0; i < index; i++) {
                    iterator.next();
                }
                component = iterator.next();

                if (component == null) {
                    component = BeanUtils.instantiate(componentType);
                }

                WebDataBinder componentBinder = binderFactory.createBinder(request, component, null);
                component = componentBinder.getTarget();

                if (component != null) {
                    ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(
                            servletRequest, prefixName, "");
                    componentBinder.bind(pvs);
                    validateIfApplicable(componentBinder, parameter);
                    if (componentBinder.getBindingResult().hasErrors()) {
                        if (isBindExceptionRequired(componentBinder, parameter)) {
                            throw new BindException(componentBinder.getBindingResult());
                        }
                    }
                    targetList.set(index, component);
                }
            }
            target.clear();
            target.addAll(targetList);
        }
    } else if (MapWapper.class.isAssignableFrom(targetType)) {

        Type type = parameter.getGenericParameterType();
        Class<?> keyType = Object.class;
        Class<?> valueType = Object.class;

        if (type instanceof ParameterizedType) {
            keyType = (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0];
            valueType = (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[1];
        }

        MapWapper mapWapper = ((MapWapper) binder.getTarget());
        Map target = mapWapper.getInnerMap();
        if (target == null) {
            target = new HashMap();
            mapWapper.setInnerMap(target);
        }

        for (Object key : servletRequest.getParameterMap().keySet()) {
            String prefixName = getPrefixName((String) key);

            //?prefix ??
            if (hasProcessedPrefixMap.containsKey(prefixName)) {
                continue;
            } else {
                hasProcessedPrefixMap.put(prefixName, Boolean.TRUE);
            }

            Object keyValue = simpleBinder.convertIfNecessary(getMapKey(prefixName), keyType);

            if (isSimpleComponent(prefixName)) { //bind simple type
                Map<String, Object> paramValues = WebUtils.getParametersStartingWith(servletRequest,
                        prefixName);

                for (Object value : paramValues.values()) {
                    target.put(keyValue, simpleBinder.convertIfNecessary(value, valueType));
                }
            } else {

                Object component = target.get(keyValue);
                if (component == null) {
                    component = BeanUtils.instantiate(valueType);
                }

                WebDataBinder componentBinder = binderFactory.createBinder(request, component, null);
                component = componentBinder.getTarget();

                if (component != null) {
                    ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(
                            servletRequest, prefixName, "");
                    componentBinder.bind(pvs);

                    validateComponent(componentBinder, parameter);

                    target.put(keyValue, component);
                }
            }
        }
    } else {//bind model
        ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
        servletBinder.bind(servletRequest);
    }
}

From source file:org.codehaus.groovy.grails.web.binding.GrailsDataBinder.java

public void bind(ServletRequest request, String prefix) {
    MutablePropertyValues mpvs;//from ww  w  . j a  v  a2  s .  c  o  m
    if (prefix != null) {
        mpvs = new ServletRequestParameterPropertyValues(request, prefix, PREFIX_SEPERATOR);
    } else {
        mpvs = new ServletRequestParameterPropertyValues(request);
    }

    bindWithRequestAndPropertyValues(request, mpvs);
}