Search in sources :

Example 1 with ModelAndViewResolver

use of org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method mavResolver.

@PathPatternsParameterizedTest
void mavResolver(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(ModelAndViewResolverController.class, usePathPatterns, wac -> {
        RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
        ModelAndViewResolver[] mavResolvers = new ModelAndViewResolver[] { new MyModelAndViewResolver() };
        adapterDef.getPropertyValues().add("modelAndViewResolvers", mavResolvers);
        wac.registerBeanDefinition("handlerAdapter", adapterDef);
    });
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myValue");
}
Also used : ModelAndViewResolver(org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 2 with ModelAndViewResolver

use of org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver in project spring-framework by spring-projects.

the class ModelAndViewResolverMethodReturnValueHandler method handleReturnValue.

@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
    if (this.mavResolvers != null) {
        for (ModelAndViewResolver mavResolver : this.mavResolvers) {
            Class<?> handlerType = returnType.getContainingClass();
            Method method = returnType.getMethod();
            Assert.state(method != null, "No handler method");
            ExtendedModelMap model = (ExtendedModelMap) mavContainer.getModel();
            ModelAndView mav = mavResolver.resolveModelAndView(method, handlerType, returnValue, model, webRequest);
            if (mav != ModelAndViewResolver.UNRESOLVED) {
                mavContainer.addAllAttributes(mav.getModel());
                mavContainer.setViewName(mav.getViewName());
                if (!mav.isReference()) {
                    mavContainer.setView(mav.getView());
                }
                return;
            }
        }
    }
    // No suitable ModelAndViewResolver...
    if (this.modelAttributeProcessor.supportsReturnType(returnType)) {
        this.modelAttributeProcessor.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
    } else {
        throw new UnsupportedOperationException("Unexpected return type: " + returnType.getParameterType().getName() + " in method: " + returnType.getMethod());
    }
}
Also used : ExtendedModelMap(org.springframework.ui.ExtendedModelMap) ModelAndViewResolver(org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver) ModelAndView(org.springframework.web.servlet.ModelAndView) Method(java.lang.reflect.Method)

Example 3 with ModelAndViewResolver

use of org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver in project com.revolsys.open by revolsys.

the class WebAnnotationMethodHandlerAdapter method getModelAndView.

@SuppressWarnings({ "unchecked", "rawtypes" })
public ModelAndView getModelAndView(final Method handlerMethod, final Class<?> handlerType, final Object returnValue, final ExtendedModelMap implicitModel, final ServletWebRequest webRequest) throws Exception {
    boolean responseArgumentUsed = false;
    final ResponseStatus responseStatusAnn = AnnotationUtils.findAnnotation(handlerMethod, ResponseStatus.class);
    if (responseStatusAnn != null) {
        final HttpStatus responseStatus = responseStatusAnn.value();
        // to be picked up by the RedirectView
        webRequest.getRequest().setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, responseStatus);
        webRequest.getResponse().setStatus(responseStatus.value());
        responseArgumentUsed = true;
    }
    // Invoke custom resolvers if present...
    if (WebAnnotationMethodHandlerAdapter.this.customModelAndViewResolvers != null) {
        for (final ModelAndViewResolver mavResolver : WebAnnotationMethodHandlerAdapter.this.customModelAndViewResolvers) {
            final ModelAndView mav = mavResolver.resolveModelAndView(handlerMethod, handlerType, returnValue, implicitModel, webRequest);
            if (mav != ModelAndViewResolver.UNRESOLVED) {
                return mav;
            }
        }
    }
    if (returnValue != null && AnnotationUtils.findAnnotation(handlerMethod, ResponseBody.class) != null) {
        final View view = handleResponseBody(returnValue, webRequest);
        return new ModelAndView(view).addAllObjects(implicitModel);
    }
    if (returnValue instanceof ModelAndView) {
        final ModelAndView mav = (ModelAndView) returnValue;
        mav.getModelMap().mergeAttributes(implicitModel);
        return mav;
    } else if (returnValue instanceof Model) {
        return new ModelAndView().addAllObjects(implicitModel).addAllObjects(((Model) returnValue).asMap());
    } else if (returnValue instanceof View) {
        return new ModelAndView((View) returnValue).addAllObjects(implicitModel);
    } else if (AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class) != null) {
        addReturnValueAsModelAttribute(handlerMethod, handlerType, returnValue, implicitModel);
        return new ModelAndView().addAllObjects(implicitModel);
    } else if (returnValue instanceof Map) {
        return new ModelAndView().addAllObjects(implicitModel).addAllObjects((Map) returnValue);
    } else if (returnValue instanceof String) {
        return new ModelAndView((String) returnValue).addAllObjects(implicitModel);
    } else if (returnValue == null) {
        // Either returned null or was 'void' return.
        if (responseArgumentUsed || webRequest.isNotModified()) {
            return null;
        } else {
            // Assuming view name translation...
            return new ModelAndView().addAllObjects(implicitModel);
        }
    } else if (!BeanUtils.isSimpleProperty(returnValue.getClass())) {
        // Assume a single model attribute...
        addReturnValueAsModelAttribute(handlerMethod, handlerType, returnValue, implicitModel);
        return new ModelAndView().addAllObjects(implicitModel);
    } else {
        throw new IllegalArgumentException("Invalid handler method return value: " + returnValue);
    }
}
Also used : ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) HttpStatus(org.springframework.http.HttpStatus) ModelAndViewResolver(org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver) ModelAndView(org.springframework.web.servlet.ModelAndView) Model(org.springframework.ui.Model) ModelAndView(org.springframework.web.servlet.ModelAndView) View(org.springframework.web.servlet.View) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) BindingAwareModelMap(org.springframework.validation.support.BindingAwareModelMap)

Aggregations

ModelAndViewResolver (org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver)3 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 HttpStatus (org.springframework.http.HttpStatus)1 Model (org.springframework.ui.Model)1 BindingAwareModelMap (org.springframework.validation.support.BindingAwareModelMap)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1 View (org.springframework.web.servlet.View)1 PathPatternsParameterizedTest (org.springframework.web.servlet.handler.PathPatternsParameterizedTest)1 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)1