Search in sources :

Example 1 with RequestContextDataBinder

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

the class DataBinderParameterResolver method resolveArgument.

/**
 * @return Pojo parameter
 */
@Override
public Object resolveArgument(final RequestContext context, final ResolvableMethodParameter resolvable) throws Throwable {
    final Class<?> parameterClass = resolvable.getParameterType();
    BeanMetadata beanMetadata = BeanMetadata.from(parameterClass);
    Object target = beanMetadata.newInstance();
    RequestContextDataBinder dataBinder = new RequestContextDataBinder(target, resolvable.getName());
    dataBinder.setConversionService(conversionService);
    dataBinder.bind(context);
    // #30 Support annotation-supported in the form of DataBinder
    resolveAnnotatedProperty(context, resolvable, dataBinder);
    // todo dataBinder.validate();
    return target;
}
Also used : BeanMetadata(cn.taketoday.beans.BeanMetadata) RequestContextDataBinder(cn.taketoday.web.bind.RequestContextDataBinder)

Example 2 with RequestContextDataBinder

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

the class ModelFactoryTests method updateModelBindingResult.

@Test
public void updateModelBindingResult() throws Throwable {
    String commandName = "attr1";
    Object command = new Object();
    RequestContextDataBinder dataBinder = new RequestContextDataBinder(command, commandName);
    BindingContext container = new BindingContext0(dataBinder);
    container.addAttribute(commandName, command);
    ModelFactory modelFactory = new ModelFactory(null, container, this.attributeHandler);
    modelFactory.updateModel(this.webRequest, container);
    assertThat(container.getModel().get(commandName)).isEqualTo(command);
    String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + commandName;
    assertThat(container.getModel().get(bindingResultKey)).isSameAs(dataBinder.getBindingResult());
    assertThat(container.getModel().size()).isEqualTo(2);
}
Also used : RequestContextDataBinder(cn.taketoday.web.bind.RequestContextDataBinder) BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Example 3 with RequestContextDataBinder

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

the class ModelFactoryTests method updateModelWhenRedirecting.

// SPR-12542
@Test
public void updateModelWhenRedirecting() throws Throwable {
    String attributeName = "sessionAttr";
    String attribute = "value";
    RequestContextDataBinder dataBinder = new RequestContextDataBinder(attribute, attributeName);
    BindingContext container = new BindingContext0(dataBinder);
    container.addAttribute(attributeName, attribute);
    String queryParam = "123";
    String queryParamName = "q";
    container.setRedirectModel(new RedirectModel(queryParamName, queryParam));
    ModelFactory modelFactory = new ModelFactory(null, container, this.attributeHandler);
    modelFactory.updateModel(this.webRequest, container);
    assertThat(container.getRedirectModel().get(queryParamName)).isEqualTo(queryParam);
    assertThat(container.getRedirectModel().size()).isEqualTo(1);
    assertThat(this.attributeStore.retrieveAttribute(this.webRequest, attributeName)).isEqualTo(attribute);
}
Also used : RedirectModel(cn.taketoday.web.view.RedirectModel) RequestContextDataBinder(cn.taketoday.web.bind.RequestContextDataBinder) BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Example 4 with RequestContextDataBinder

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

the class RequestParamMethodArgumentResolverTests method missingRequestParamEmptyValueConvertedToNull.

// SPR-10578
@Test
public void missingRequestParamEmptyValueConvertedToNull() throws Throwable {
    RequestContextDataBinder binder = new RequestContextDataBinder(null);
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    ServletRequestContext webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
    BindingContext binderFactory = mock(BindingContext.class);
    given(binderFactory.createBinder(webRequest, null, "stringNotAnnot")).willReturn(binder);
    given(binderFactory.createBinder(webRequest, "stringNotAnnot")).willReturn(binder);
    webRequest.setBindingContext(binderFactory);
    request.addParameter("stringNotAnnot", "");
    ResolvableMethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
    // Object arg = resolver.resolveArgument(webRequest, param);
    // assertThat(arg).isNull();
    assertThatThrownBy(() -> resolver.resolveArgument(webRequest, param)).isInstanceOf(MissingRequestParameterException.class).hasMessage("Required request parameter 'stringNotAnnot' for method parameter type String is present but converted to null");
}
Also used : RequestParam(cn.taketoday.web.annotation.RequestParam) RequestContextDataBinder(cn.taketoday.web.bind.RequestContextDataBinder) StringTrimmerEditor(cn.taketoday.beans.propertyeditors.StringTrimmerEditor) MissingRequestParameterException(cn.taketoday.web.bind.MissingRequestParameterException) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) BindingContext(cn.taketoday.web.BindingContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 5 with RequestContextDataBinder

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

the class RequestParamMethodArgumentResolverTests method missingRequestParamEmptyValueNotRequired.

@Test
public void missingRequestParamEmptyValueNotRequired() throws Throwable {
    RequestContextDataBinder binder = new RequestContextDataBinder(null);
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    request.addParameter("name", "");
    ServletRequestContext webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
    BindingContext binderFactory = mock(BindingContext.class);
    given(binderFactory.createBinder(webRequest, null, "name")).willReturn(binder);
    given(binderFactory.createBinder(webRequest, "name")).willReturn(binder);
    webRequest.setBindingContext(binderFactory);
    ResolvableMethodParameter param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
    Object arg = resolver.resolveArgument(webRequest, param);
    assertThat(arg).isNull();
}
Also used : RequestContextDataBinder(cn.taketoday.web.bind.RequestContextDataBinder) StringTrimmerEditor(cn.taketoday.beans.propertyeditors.StringTrimmerEditor) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) BindingContext(cn.taketoday.web.BindingContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

RequestContextDataBinder (cn.taketoday.web.bind.RequestContextDataBinder)18 BindingContext (cn.taketoday.web.BindingContext)9 Test (org.junit.jupiter.api.Test)8 BeanMetadata (cn.taketoday.beans.BeanMetadata)7 PropertyValue (cn.taketoday.beans.PropertyValue)4 PropertyValues (cn.taketoday.beans.PropertyValues)4 MultiValueMap (cn.taketoday.core.MultiValueMap)4 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)4 List (java.util.List)4 Map (java.util.Map)4 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)3 StringTrimmerEditor (cn.taketoday.beans.propertyeditors.StringTrimmerEditor)2 MethodParameter (cn.taketoday.core.MethodParameter)2 ResolvableType (cn.taketoday.core.ResolvableType)2 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)2 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)1 DefaultFormattingConversionService (cn.taketoday.format.support.DefaultFormattingConversionService)1 RequestParam (cn.taketoday.web.annotation.RequestParam)1 MissingRequestParameterException (cn.taketoday.web.bind.MissingRequestParameterException)1 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)1