Search in sources :

Example 71 with BindingContext

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

the class ModelFactoryTests method updateModelSessionAttributesRemoved.

@Test
public void updateModelSessionAttributesRemoved() throws Throwable {
    String attributeName = "sessionAttr";
    String attribute = "value";
    RequestContextDataBinder dataBinder = new RequestContextDataBinder(attribute, attributeName);
    BindingContext container = new BindingContext0(dataBinder);
    container.addAttribute(attributeName, attribute);
    this.attributeStore.storeAttribute(this.webRequest, attributeName, attribute);
    container.getSessionStatus().setComplete();
    ModelFactory modelFactory = new ModelFactory(null, container, this.attributeHandler);
    modelFactory.updateModel(this.webRequest, container);
    assertThat(container.getModel().get(attributeName)).isEqualTo(attribute);
    assertThat(this.attributeStore.retrieveAttribute(this.webRequest, attributeName)).isNull();
}
Also used : RequestContextDataBinder(cn.taketoday.web.bind.RequestContextDataBinder) BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Example 72 with BindingContext

use of cn.taketoday.web.BindingContext in project today-infrastructure 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 73 with BindingContext

use of cn.taketoday.web.BindingContext in project today-infrastructure by TAKETODAY.

the class ErrorsMethodArgumentResolver method resolveArgument.

@Nullable
@Override
public Object resolveArgument(RequestContext context, ResolvableMethodParameter resolvable) throws Throwable {
    BindingContext bindingContext = context.getBindingContext();
    ModelMap model = bindingContext.getModel();
    String lastKey = CollectionUtils.lastElement(model.keySet());
    if (lastKey != null && lastKey.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
        return model.getAttribute(lastKey);
    }
    throw new IllegalStateException("An Errors/BindingResult argument is expected to be declared immediately after " + "the model attribute, the @RequestBody or the @RequestPart arguments " + "to which they apply: " + resolvable.getMethod());
}
Also used : ModelMap(cn.taketoday.web.view.ModelMap) BindingContext(cn.taketoday.web.BindingContext) Nullable(cn.taketoday.lang.Nullable)

Example 74 with BindingContext

use of cn.taketoday.web.BindingContext in project today-infrastructure by TAKETODAY.

the class VoidReturnValueHandler method handleReturnValue.

/**
 * mainly for ModelAndView
 *
 * @param context Current HTTP request context
 * @param handler Target HTTP handler
 * @param returnValue Handler execution result
 * Or {@link HandlerExceptionHandler} return value
 */
@Override
public void handleReturnValue(RequestContext context, Object handler, @Nullable Object returnValue) throws Exception {
    BindingContext bindingContext = context.getBindingContext();
    if (bindingContext.hasModelAndView()) {
        ModelAndView modelAndView = bindingContext.getModelAndView();
        // user constructed a ModelAndView hold in context
        returnValueHandler.handleModelAndView(context, null, modelAndView);
    }
}
Also used : ModelAndView(cn.taketoday.web.view.ModelAndView) BindingContext(cn.taketoday.web.BindingContext)

Example 75 with BindingContext

use of cn.taketoday.web.BindingContext in project today-infrastructure by TAKETODAY.

the class ReactiveTypeHandlerTests method handleValue.

private ResponseBodyEmitter handleValue(Object returnValue, Class<?> asyncType, ResolvableType genericType) throws Exception {
    BindingContext mavContainer = new BindingContext();
    MethodParameter returnType = on(TestController.class).resolveReturnType(asyncType, genericType);
    webRequest.setBindingContext(mavContainer);
    return this.handler.handleValue(returnValue, returnType, this.webRequest);
}
Also used : BindingContext(cn.taketoday.web.BindingContext) MethodParameter(cn.taketoday.core.MethodParameter)

Aggregations

BindingContext (cn.taketoday.web.BindingContext)75 Test (org.junit.jupiter.api.Test)53 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)31 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)30 RequestParam (cn.taketoday.web.annotation.RequestParam)23 ConfigurableWebBindingInitializer (cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer)17 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)13 DefaultConversionService (cn.taketoday.core.conversion.support.DefaultConversionService)11 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)11 RequestContextDataBinder (cn.taketoday.web.bind.RequestContextDataBinder)9 MockMultipartFile (cn.taketoday.web.testfixture.servlet.MockMultipartFile)9 MockMultipartHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockMultipartHttpServletRequest)9 MultipartFile (cn.taketoday.web.multipart.MultipartFile)8 DefaultFormattingConversionService (cn.taketoday.format.support.DefaultFormattingConversionService)7 Nullable (cn.taketoday.lang.Nullable)7 Optional (java.util.Optional)7 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)6 MethodParameter (cn.taketoday.core.MethodParameter)6 MockPart (cn.taketoday.web.testfixture.servlet.MockPart)6 RequestPart (cn.taketoday.web.annotation.RequestPart)5