Search in sources :

Example 6 with WebDataBinder

use of cn.taketoday.web.bind.WebDataBinder in project today-framework by TAKETODAY.

the class ModelFactory method updateBindingResult.

/**
 * Add {@link BindingResult} attributes to the model for attributes that require it.
 */
private void updateBindingResult(RequestContext request, Map<String, Object> model) throws Throwable {
    ArrayList<String> keyNames = new ArrayList<>(model.keySet());
    for (String name : keyNames) {
        Object value = model.get(name);
        if (value != null && isBindingCandidate(name, value)) {
            String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + name;
            if (!model.containsKey(bindingResultKey)) {
                WebDataBinder dataBinder = bindingContext.createBinder(request, value, name);
                model.put(bindingResultKey, dataBinder.getBindingResult());
            }
        }
    }
}
Also used : WebDataBinder(cn.taketoday.web.bind.WebDataBinder) ArrayList(java.util.ArrayList)

Example 7 with WebDataBinder

use of cn.taketoday.web.bind.WebDataBinder in project today-framework by TAKETODAY.

the class RequestResponseBodyMethodProcessorMockTests method initBindingContext.

public void initBindingContext() {
    BindingContext bindingContext = new BindingContext() {

        @Override
        public void initBinder(WebDataBinder dataBinder, RequestContext request) {
            LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
            validator.afterPropertiesSet();
            dataBinder.setValidator(validator);
        }
    };
    webRequest.setBindingContext(bindingContext);
}
Also used : LocalValidatorFactoryBean(cn.taketoday.validation.beanvalidation.LocalValidatorFactoryBean) WebDataBinder(cn.taketoday.web.bind.WebDataBinder) RequestContext(cn.taketoday.web.RequestContext) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) BindingContext(cn.taketoday.web.BindingContext)

Example 8 with WebDataBinder

use of cn.taketoday.web.bind.WebDataBinder in project today-framework by TAKETODAY.

the class AbstractNamedValueResolvingStrategy method resolveArgument.

@Nullable
@Override
public final Object resolveArgument(RequestContext context, ResolvableMethodParameter resolvable) throws Throwable {
    MethodParameter methodParameter = resolvable.getParameter();
    NamedValueInfo namedValueInfo = getNamedValueInfo(resolvable);
    MethodParameter nestedParameter = methodParameter.nestedIfOptional();
    Object arg;
    if (namedValueInfo.nameEmbedded) {
        Object resolvedName = resolveEmbeddedValuesAndExpressions(namedValueInfo.name);
        if (resolvedName == null) {
            throw new IllegalArgumentException("Specified name must not resolve to null: [" + namedValueInfo.name + "]");
        }
        arg = resolveName(resolvedName.toString(), resolvable, context);
    } else {
        arg = resolveName(namedValueInfo.name, resolvable, context);
    }
    if (arg == null) {
        if (namedValueInfo.defaultValue != null) {
            arg = resolveEmbeddedValuesAndExpressions(namedValueInfo.defaultValue);
        } else if (namedValueInfo.required && !nestedParameter.isOptional()) {
            handleMissingValue(namedValueInfo.name, nestedParameter, context);
        }
        arg = handleNullValue(namedValueInfo.name, arg, nestedParameter.getNestedParameterType());
    } else if ("".equals(arg) && namedValueInfo.defaultValue != null) {
        arg = resolveEmbeddedValuesAndExpressions(namedValueInfo.defaultValue);
    }
    BindingContext bindingContext = context.getBindingContext();
    if (bindingContext != null) {
        WebDataBinder binder = bindingContext.createBinder(context, namedValueInfo.name);
        try {
            arg = binder.convertIfNecessary(arg, methodParameter.getParameterType(), methodParameter);
        } catch (ConversionNotSupportedException ex) {
            throw new MethodArgumentConversionNotSupportedException(arg, ex.getRequiredType(), namedValueInfo.name, methodParameter, ex.getCause());
        } catch (TypeMismatchException ex) {
            throw new MethodArgumentTypeMismatchException(arg, ex.getRequiredType(), namedValueInfo.name, methodParameter, ex.getCause());
        }
        // Check for null value after conversion of incoming argument value
        if (arg == null && namedValueInfo.defaultValue == null && namedValueInfo.required && !nestedParameter.isOptional()) {
            handleMissingValueAfterConversion(namedValueInfo.name, nestedParameter, context);
        }
    }
    handleResolvedValue(arg, namedValueInfo.name, resolvable, context);
    return arg;
}
Also used : WebDataBinder(cn.taketoday.web.bind.WebDataBinder) MethodArgumentTypeMismatchException(cn.taketoday.web.handler.method.MethodArgumentTypeMismatchException) NamedValueInfo(cn.taketoday.web.handler.method.NamedValueInfo) TypeMismatchException(cn.taketoday.beans.TypeMismatchException) MethodArgumentTypeMismatchException(cn.taketoday.web.handler.method.MethodArgumentTypeMismatchException) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) BindingContext(cn.taketoday.web.BindingContext) MethodArgumentConversionNotSupportedException(cn.taketoday.web.handler.method.MethodArgumentConversionNotSupportedException) ConversionNotSupportedException(cn.taketoday.beans.ConversionNotSupportedException) MethodArgumentConversionNotSupportedException(cn.taketoday.web.handler.method.MethodArgumentConversionNotSupportedException) Nullable(cn.taketoday.lang.Nullable)

Example 9 with WebDataBinder

use of cn.taketoday.web.bind.WebDataBinder in project today-framework by TAKETODAY.

the class InitBinderDataBinderFactoryTests method createBinderWithAttrNameNoMatch.

@Test
public void createBinderWithAttrNameNoMatch() throws Throwable {
    InitBinderBindingContext factory = createFactory("initBinderWithAttributeName", WebDataBinder.class);
    WebDataBinder dataBinder = factory.createBinder(this.webRequest, null, "invalidName");
    assertThat(dataBinder.getDisallowedFields()).isNull();
}
Also used : WebDataBinder(cn.taketoday.web.bind.WebDataBinder) Test(org.junit.jupiter.api.Test)

Example 10 with WebDataBinder

use of cn.taketoday.web.bind.WebDataBinder in project today-framework by TAKETODAY.

the class InitBinderDataBinderFactoryTests method createBinderTypeConversion.

@Test
public void createBinderTypeConversion() throws Throwable {
    request.setParameter("requestParam", "22");
    this.argumentResolvers.addStrategies(new RequestParamMethodArgumentResolver(null, false));
    InitBinderBindingContext factory = createFactory("initBinderTypeConversion", WebDataBinder.class, int.class);
    WebDataBinder dataBinder = factory.createBinder(this.webRequest, null, "foo");
    assertThat(dataBinder.getDisallowedFields()).isNotNull();
    assertThat(dataBinder.getDisallowedFields()[0]).isEqualToIgnoringCase("requestParam-22");
}
Also used : RequestParamMethodArgumentResolver(cn.taketoday.web.bind.resolver.RequestParamMethodArgumentResolver) WebDataBinder(cn.taketoday.web.bind.WebDataBinder) Test(org.junit.jupiter.api.Test)

Aggregations

WebDataBinder (cn.taketoday.web.bind.WebDataBinder)13 Test (org.junit.jupiter.api.Test)6 MethodParameter (cn.taketoday.core.MethodParameter)4 BindingContext (cn.taketoday.web.BindingContext)4 Nullable (cn.taketoday.lang.Nullable)3 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)3 ConversionNotSupportedException (cn.taketoday.beans.ConversionNotSupportedException)2 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)2 MethodArgumentConversionNotSupportedException (cn.taketoday.web.handler.method.MethodArgumentConversionNotSupportedException)2 MethodArgumentTypeMismatchException (cn.taketoday.web.handler.method.MethodArgumentTypeMismatchException)2 NamedValueInfo (cn.taketoday.web.handler.method.NamedValueInfo)2 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)2 ArrayList (java.util.ArrayList)2 ConversionService (cn.taketoday.core.conversion.ConversionService)1 DefaultFormattingConversionService (cn.taketoday.format.support.DefaultFormattingConversionService)1 Errors (cn.taketoday.validation.Errors)1 LocalValidatorFactoryBean (cn.taketoday.validation.beanvalidation.LocalValidatorFactoryBean)1 RequestContext (cn.taketoday.web.RequestContext)1 RequestPart (cn.taketoday.web.annotation.RequestPart)1 MethodArgumentNotValidException (cn.taketoday.web.bind.MethodArgumentNotValidException)1