Example usage for org.springframework.validation DataBinder getConversionService

List of usage examples for org.springframework.validation DataBinder getConversionService

Introduction

In this page you can find the example usage for org.springframework.validation DataBinder getConversionService.

Prototype

@Nullable
public ConversionService getConversionService() 

Source Link

Document

Return the associated ConversionService, if any.

Usage

From source file:com.fengduo.bee.commons.component.FormModelMethodArgumentResolver.java

/**
 * Create a model attribute from a String request value (e.g. URI template variable, request parameter) using type
 * conversion./*from  w  w w . j  a  v a2  s. c o  m*/
 * <p>
 * The default implementation converts only if there a registered
 * {@link org.springframework.core.convert.converter.Converter} that can perform the conversion.
 * 
 * @param sourceValue the source value to create the model attribute from
 * @param attributeName the name of the attribute, never {@code null}
 * @param parameter the method parameter
 * @param binderFactory for creating WebDataBinder instance
 * @param request the current request
 * @return the created model attribute, or {@code null}
 * @throws Exception
 */
protected Object createAttributeFromRequestValue(String sourceValue, String attributeName,
        MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request)
        throws Exception {
    DataBinder binder = binderFactory.createBinder(request, null, attributeName);
    ConversionService conversionService = binder.getConversionService();
    if (conversionService != null) {
        TypeDescriptor source = TypeDescriptor.valueOf(String.class);
        TypeDescriptor target = new TypeDescriptor(parameter);
        if (conversionService.canConvert(source, target)) {
            return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter);
        }
    }
    return null;
}