Search in sources :

Example 21 with WebDataBinderFactory

use of org.springframework.web.bind.support.WebDataBinderFactory in project spring-framework by spring-projects.

the class ModelFactoryOrderingTests method runTest.

private void runTest(Object controller) throws Exception {
    HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite();
    resolvers.addResolver(new ModelAttributeMethodProcessor(false));
    resolvers.addResolver(new ModelMethodProcessor());
    WebDataBinderFactory dataBinderFactory = new DefaultDataBinderFactory(null);
    Class<?> type = controller.getClass();
    Set<Method> methods = MethodIntrospector.selectMethods(type, METHOD_FILTER);
    List<InvocableHandlerMethod> modelMethods = new ArrayList<>();
    for (Method method : methods) {
        InvocableHandlerMethod modelMethod = new InvocableHandlerMethod(controller, method);
        modelMethod.setHandlerMethodArgumentResolvers(resolvers);
        modelMethod.setDataBinderFactory(dataBinderFactory);
        modelMethods.add(modelMethod);
    }
    Collections.shuffle(modelMethods);
    SessionAttributesHandler sessionHandler = new SessionAttributesHandler(type, this.sessionAttributeStore);
    ModelFactory factory = new ModelFactory(modelMethods, dataBinderFactory, sessionHandler);
    factory.initModel(this.webRequest, this.mavContainer, new HandlerMethod(controller, "handle"));
    if (logger.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder();
        for (String name : getInvokedMethods()) {
            sb.append(" >> ").append(name);
        }
        logger.debug(sb);
    }
}
Also used : InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) ArrayList(java.util.ArrayList) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) HandlerMethodArgumentResolverComposite(org.springframework.web.method.support.HandlerMethodArgumentResolverComposite) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory)

Example 22 with WebDataBinderFactory

use of org.springframework.web.bind.support.WebDataBinderFactory in project spring-framework by spring-projects.

the class ModelFactoryTests method updateModelBindingResult.

@Test
public void updateModelBindingResult() throws Exception {
    String commandName = "attr1";
    Object command = new Object();
    ModelAndViewContainer container = new ModelAndViewContainer();
    container.addAttribute(commandName, command);
    WebDataBinder dataBinder = new WebDataBinder(command, commandName);
    WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
    given(binderFactory.createBinder(this.webRequest, command, commandName)).willReturn(dataBinder);
    ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler);
    modelFactory.updateModel(this.webRequest, container);
    assertEquals(command, container.getModel().get(commandName));
    String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + commandName;
    assertSame(dataBinder.getBindingResult(), container.getModel().get(bindingResultKey));
    assertEquals(2, container.getModel().size());
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 23 with WebDataBinderFactory

use of org.springframework.web.bind.support.WebDataBinderFactory in project spring-framework by spring-projects.

the class InitBinderDataBinderFactoryTests method createBinder.

@Test
public void createBinder() throws Exception {
    WebDataBinderFactory factory = createFactory("initBinder", WebDataBinder.class);
    WebDataBinder dataBinder = factory.createBinder(this.webRequest, null, null);
    assertNotNull(dataBinder.getDisallowedFields());
    assertEquals("id", dataBinder.getDisallowedFields()[0]);
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 24 with WebDataBinderFactory

use of org.springframework.web.bind.support.WebDataBinderFactory in project spring-framework by spring-projects.

the class InitBinderDataBinderFactoryTests method createBinderTypeConversion.

@Test
public void createBinderTypeConversion() throws Exception {
    this.webRequest.getNativeRequest(MockHttpServletRequest.class).setParameter("requestParam", "22");
    this.argumentResolvers.addResolver(new RequestParamMethodArgumentResolver(null, false));
    WebDataBinderFactory factory = createFactory("initBinderTypeConversion", WebDataBinder.class, int.class);
    WebDataBinder dataBinder = factory.createBinder(this.webRequest, null, "foo");
    assertNotNull(dataBinder.getDisallowedFields());
    assertEquals("requestParam-22", dataBinder.getDisallowedFields()[0]);
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 25 with WebDataBinderFactory

use of org.springframework.web.bind.support.WebDataBinderFactory in project spring-framework by spring-projects.

the class InitBinderDataBinderFactoryTests method createBinderWithAttrName.

@Test
public void createBinderWithAttrName() throws Exception {
    WebDataBinderFactory factory = createFactory("initBinderWithAttributeName", WebDataBinder.class);
    WebDataBinder dataBinder = factory.createBinder(this.webRequest, null, "foo");
    assertNotNull(dataBinder.getDisallowedFields());
    assertEquals("id", dataBinder.getDisallowedFields()[0]);
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Aggregations

WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)29 Test (org.junit.Test)26 WebDataBinder (org.springframework.web.bind.WebDataBinder)15 MethodParameter (org.springframework.core.MethodParameter)7 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)7 DefaultDataBinderFactory (org.springframework.web.bind.support.DefaultDataBinderFactory)7 TestBean (org.springframework.tests.sample.beans.TestBean)6 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)6 RequestParam (org.springframework.web.bind.annotation.RequestParam)5 WebRequestDataBinder (org.springframework.web.bind.support.WebRequestDataBinder)5 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)5 Optional (java.util.Optional)2 StringTrimmerEditor (org.springframework.beans.propertyeditors.StringTrimmerEditor)2 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)1 ConversionService (org.springframework.core.convert.ConversionService)1