Search in sources :

Example 11 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);
        Assert.state(ann != null, "No ModelAttribute annotation");
        if (container.containsAttribute(ann.name())) {
            if (!ann.binding()) {
                container.setBindingDisabled(ann.name());
            }
            continue;
        }
        Object returnValue = modelMethod.invokeForRequest(request, container);
        if (modelMethod.isVoid()) {
            if (StringUtils.hasText(ann.value())) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Name in @ModelAttribute is ignored because method returns void: " + modelMethod.getShortLogMessage());
                }
            }
            continue;
        }
        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)

Example 12 with InvocableHandlerMethod

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

the class MvcNamespaceTests method setup.

@BeforeEach
public void setup() throws Exception {
    TestMockServletContext servletContext = new TestMockServletContext();
    appContext = new XmlWebApplicationContext();
    appContext.setServletContext(servletContext);
    LocaleContextHolder.setLocale(Locale.US);
    String attributeName = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
    appContext.getServletContext().setAttribute(attributeName, appContext);
    handler = new TestController();
    handlerMethod = new InvocableHandlerMethod(handler, TestController.class.getMethod("testBind", Date.class, Double.class, TestBean.class, BindingResult.class));
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

InvocableHandlerMethod (org.springframework.web.method.support.InvocableHandlerMethod)12 HandlerMethod (org.springframework.web.method.HandlerMethod)6 Method (java.lang.reflect.Method)5 ArrayList (java.util.ArrayList)3 DefaultDataBinderFactory (org.springframework.web.bind.support.DefaultDataBinderFactory)3 LocalVariableTableParameterNameDiscoverer (org.springframework.core.LocalVariableTableParameterNameDiscoverer)2 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)2 HandlerMethodArgumentResolverComposite (org.springframework.web.method.support.HandlerMethodArgumentResolverComposite)2 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)2 Before (org.junit.Before)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpMethod (org.springframework.http.HttpMethod)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 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)1 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)1 ModelFactory (org.springframework.web.method.annotation.ModelFactory)1 SessionAttributesHandler (org.springframework.web.method.annotation.SessionAttributesHandler)1