Java Code Examples for org.springframework.validation.DataBinder#convertIfNecessary()

The following examples show how to use org.springframework.validation.DataBinder#convertIfNecessary() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServletModelAttributeMethodProcessor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <p>The default implementation converts only if there a registered
 * {@link 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} if no suitable
 * conversion found
 */
@Nullable
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;
}
 
Example 2
Source File: ServletModelAttributeMethodProcessor.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <p>The default implementation converts only if there a registered
 * {@link 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} if no suitable
 * conversion found
 */
@Nullable
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;
}
 
Example 3
Source File: FormModelMethodArgumentResolver.java    From distributed-transaction-process with MIT License 6 votes vote down vote up
/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <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;
}
 
Example 4
Source File: ServletModelAttributeMethodProcessor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <p>The default implementation converts only if there a registered
 * {@link 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 methodParam the method parameter
 * @param binderFactory for creating WebDataBinder instance
 * @param request the current request
 * @return the created model attribute, or {@code null} if no suitable
 * conversion found
 * @throws Exception
 */
protected Object createAttributeFromRequestValue(String sourceValue, String attributeName,
		MethodParameter methodParam, 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(methodParam);
		if (conversionService.canConvert(source, target)) {
			return binder.convertIfNecessary(sourceValue, methodParam.getParameterType(), methodParam);
		}
	}
	return null;
}
 
Example 5
Source File: ServletModelAttributeMethodProcessor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <p>The default implementation converts only if there a registered
 * {@link 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 methodParam 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 methodParam, 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(methodParam);
		if (conversionService.canConvert(source, target)) {
			return binder.convertIfNecessary(sourceValue, methodParam.getParameterType(), methodParam);
		}
	}
	return null;
}
 
Example 6
Source File: FormModelMethodArgumentResolver.java    From es with Apache License 2.0 6 votes vote down vote up
/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <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;
}
 
Example 7
Source File: EntityModelAttributeMethodProcessor.java    From java-platform with Apache License 2.0 4 votes vote down vote up
/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <p>
 * The default implementation converts only if there a registered
 * {@link 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 methodParam
 *            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 methodParam, 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(methodParam);
		if (conversionService.canConvert(source, target)) {
			return binder.convertIfNecessary(sourceValue, methodParam.getParameterType(), methodParam);
		}
	}
	return null;
}