Search in sources :

Example 41 with ModelAndViewContainer

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

the class ModelMethodProcessorTests method setUp.

@BeforeEach
public void setUp() throws Exception {
    processor = new ModelMethodProcessor();
    mavContainer = new ModelAndViewContainer();
    Method method = getClass().getDeclaredMethod("model", Model.class);
    paramModel = new MethodParameter(method, 0);
    returnParamModel = new MethodParameter(method, -1);
    webRequest = new ServletWebRequest(new MockHttpServletRequest());
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 42 with ModelAndViewContainer

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

the class ExceptionHandlerExceptionResolver method doResolveHandlerMethodException.

/**
 * Find an {@code @ExceptionHandler} method and invoke it to handle the raised exception.
 */
@Override
@Nullable
protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request, HttpServletResponse response, @Nullable HandlerMethod handlerMethod, Exception exception) {
    ServletInvocableHandlerMethod exceptionHandlerMethod = getExceptionHandlerMethod(handlerMethod, exception);
    if (exceptionHandlerMethod == null) {
        return null;
    }
    if (this.argumentResolvers != null) {
        exceptionHandlerMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
    }
    if (this.returnValueHandlers != null) {
        exceptionHandlerMethod.setHandlerMethodReturnValueHandlers(this.returnValueHandlers);
    }
    ServletWebRequest webRequest = new ServletWebRequest(request, response);
    ModelAndViewContainer mavContainer = new ModelAndViewContainer();
    ArrayList<Throwable> exceptions = new ArrayList<>();
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Using @ExceptionHandler " + exceptionHandlerMethod);
        }
        // Expose causes as provided arguments as well
        Throwable exToExpose = exception;
        while (exToExpose != null) {
            exceptions.add(exToExpose);
            Throwable cause = exToExpose.getCause();
            exToExpose = (cause != exToExpose ? cause : null);
        }
        Object[] arguments = new Object[exceptions.size() + 1];
        // efficient arraycopy call in ArrayList
        exceptions.toArray(arguments);
        arguments[arguments.length - 1] = handlerMethod;
        exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, arguments);
    } catch (Throwable invocationEx) {
        // probably an accident (e.g. failed assertion or the like).
        if (!exceptions.contains(invocationEx) && logger.isWarnEnabled()) {
            logger.warn("Failure in @ExceptionHandler " + exceptionHandlerMethod, invocationEx);
        }
        // Continue with default processing of the original exception...
        return null;
    }
    if (mavContainer.isRequestHandled()) {
        return new ModelAndView();
    } else {
        ModelMap model = mavContainer.getModel();
        HttpStatus status = mavContainer.getStatus();
        ModelAndView mav = new ModelAndView(mavContainer.getViewName(), model, status);
        mav.setViewName(mavContainer.getViewName());
        if (!mavContainer.isViewReference()) {
            mav.setView((View) mavContainer.getView());
        }
        if (model instanceof RedirectAttributes) {
            Map<String, ?> flashAttributes = ((RedirectAttributes) model).getFlashAttributes();
            RequestContextUtils.getOutputFlashMap(request).putAll(flashAttributes);
        }
        return mav;
    }
}
Also used : HttpStatus(org.springframework.http.HttpStatus) ModelMap(org.springframework.ui.ModelMap) ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectAttributes(org.springframework.web.servlet.mvc.support.RedirectAttributes) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Nullable(org.springframework.lang.Nullable)

Example 43 with ModelAndViewContainer

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

the class HttpEntityMethodProcessorTests method setup.

@BeforeEach
public void setup() throws Exception {
    Method method = getClass().getDeclaredMethod("handle", HttpEntity.class, HttpEntity.class);
    paramList = new MethodParameter(method, 0);
    paramSimpleBean = new MethodParameter(method, 1);
    mavContainer = new ModelAndViewContainer();
    binderFactory = new ValidatingBinderFactory();
    servletRequest = new MockHttpServletRequest();
    servletResponse = new MockHttpServletResponse();
    servletRequest.setMethod("POST");
    webRequest = new ServletWebRequest(servletRequest, servletResponse);
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 44 with ModelAndViewContainer

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

the class ViewNameMethodReturnValueHandlerTests method setup.

@BeforeEach
public void setup() throws NoSuchMethodException {
    this.handler = new ViewNameMethodReturnValueHandler();
    this.mavContainer = new ModelAndViewContainer();
    this.webRequest = new ServletWebRequest(new MockHttpServletRequest());
    this.param = new MethodParameter(getClass().getDeclaredMethod("viewName"), -1);
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)44 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)29 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)22 BeforeEach (org.junit.jupiter.api.BeforeEach)20 MethodParameter (org.springframework.core.MethodParameter)14 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)12 Method (java.lang.reflect.Method)10 Test (org.junit.jupiter.api.Test)10 WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)5 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)4 HandlerMethod (org.springframework.web.method.HandlerMethod)4 HttpMethod (org.springframework.http.HttpMethod)3 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)3 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)3 WebDataBinder (org.springframework.web.bind.WebDataBinder)3 LinkedHashMap (java.util.LinkedHashMap)2 Before (org.junit.Before)2 Test (org.junit.Test)2 HttpInputMessage (org.springframework.http.HttpInputMessage)2 Nullable (org.springframework.lang.Nullable)2