Search in sources :

Example 6 with InvocableHandlerMethod

use of org.springframework.web.method.support.InvocableHandlerMethod in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapter method getDataBinderFactory.

private WebDataBinderFactory getDataBinderFactory(HandlerMethod handlerMethod) throws Exception {
    Class<?> handlerType = handlerMethod.getBeanType();
    Set<Method> methods = this.initBinderCache.get(handlerType);
    if (methods == null) {
        methods = MethodIntrospector.selectMethods(handlerType, INIT_BINDER_METHODS);
        this.initBinderCache.put(handlerType, methods);
    }
    List<InvocableHandlerMethod> initBinderMethods = new ArrayList<>();
    // Global methods first
    for (Entry<ControllerAdviceBean, Set<Method>> entry : this.initBinderAdviceCache.entrySet()) {
        if (entry.getKey().isApplicableToBeanType(handlerType)) {
            Object bean = entry.getKey().resolveBean();
            for (Method method : entry.getValue()) {
                initBinderMethods.add(createInitBinderMethod(bean, method));
            }
        }
    }
    for (Method method : methods) {
        Object bean = handlerMethod.getBean();
        initBinderMethods.add(createInitBinderMethod(bean, method));
    }
    return createDataBinderFactory(initBinderMethods);
}
Also used : Set(java.util.Set) 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) ControllerAdviceBean(org.springframework.web.method.ControllerAdviceBean)

Example 7 with InvocableHandlerMethod

use of org.springframework.web.method.support.InvocableHandlerMethod in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapter method createInitBinderMethod.

private InvocableHandlerMethod createInitBinderMethod(Object bean, Method method) {
    InvocableHandlerMethod binderMethod = new InvocableHandlerMethod(bean, method);
    binderMethod.setHandlerMethodArgumentResolvers(this.initBinderArgumentResolvers);
    binderMethod.setDataBinderFactory(new DefaultDataBinderFactory(this.webBindingInitializer));
    binderMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);
    return binderMethod;
}
Also used : InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory)

Example 8 with InvocableHandlerMethod

use of org.springframework.web.method.support.InvocableHandlerMethod 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 9 with InvocableHandlerMethod

use of org.springframework.web.method.support.InvocableHandlerMethod in project spring-framework by spring-projects.

the class ModelFactoryTests method createModelFactory.

private ModelFactory createModelFactory(String methodName, Class<?>... parameterTypes) throws Exception {
    HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite();
    resolvers.addResolver(new ModelMethodProcessor());
    InvocableHandlerMethod modelMethod = createHandlerMethod(methodName, parameterTypes);
    modelMethod.setHandlerMethodArgumentResolvers(resolvers);
    modelMethod.setDataBinderFactory(null);
    modelMethod.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());
    return new ModelFactory(Collections.singletonList(modelMethod), null, this.attributeHandler);
}
Also used : LocalVariableTableParameterNameDiscoverer(org.springframework.core.LocalVariableTableParameterNameDiscoverer) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethodArgumentResolverComposite(org.springframework.web.method.support.HandlerMethodArgumentResolverComposite)

Example 10 with InvocableHandlerMethod

use of org.springframework.web.method.support.InvocableHandlerMethod in project spring-framework by spring-projects.

the class ModelFactory method invokeModelAttributeMethods.

/**
	 * Invoke model attribute methods to populate the model.
	 * Attributes are added only if not already present in the model.
	 */
private void invokeModelAttributeMethods(NativeWebRequest request, ModelAndViewContainer container) throws Exception {
    while (!this.modelMethods.isEmpty()) {
        InvocableHandlerMethod modelMethod = getNextModelMethod(container).getHandlerMethod();
        ModelAttribute ann = modelMethod.getMethodAnnotation(ModelAttribute.class);
        if (container.containsAttribute(ann.name())) {
            if (!ann.binding()) {
                container.setBindingDisabled(ann.name());
            }
            continue;
        }
        Object returnValue = modelMethod.invokeForRequest(request, container);
        if (!modelMethod.isVoid()) {
            String returnValueName = getNameForReturnValue(returnValue, modelMethod.getReturnType());
            if (!ann.binding()) {
                container.setBindingDisabled(returnValueName);
            }
            if (!container.containsAttribute(returnValueName)) {
                container.addAttribute(returnValueName, returnValue);
            }
        }
    }
}
Also used : InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute)

Aggregations

InvocableHandlerMethod (org.springframework.web.method.support.InvocableHandlerMethod)10 Method (java.lang.reflect.Method)5 HandlerMethod (org.springframework.web.method.HandlerMethod)5 ArrayList (java.util.ArrayList)3 DefaultDataBinderFactory (org.springframework.web.bind.support.DefaultDataBinderFactory)3 Set (java.util.Set)2 LocalVariableTableParameterNameDiscoverer (org.springframework.core.LocalVariableTableParameterNameDiscoverer)2 ControllerAdviceBean (org.springframework.web.method.ControllerAdviceBean)2 HandlerMethodArgumentResolverComposite (org.springframework.web.method.support.HandlerMethodArgumentResolverComposite)2 Before (org.junit.Before)1 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)1 ModelAttribute (org.springframework.web.bind.annotation.ModelAttribute)1 WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)1 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)1 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)1 ModelFactory (org.springframework.web.method.annotation.ModelFactory)1 SessionAttributesHandler (org.springframework.web.method.annotation.SessionAttributesHandler)1 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)1