Search in sources :

Example 36 with ModelMap

use of org.springframework.ui.ModelMap 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
protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod, Exception exception) {
    ServletInvocableHandlerMethod exceptionHandlerMethod = getExceptionHandlerMethod(handlerMethod, exception);
    if (exceptionHandlerMethod == null) {
        return null;
    }
    exceptionHandlerMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
    exceptionHandlerMethod.setHandlerMethodReturnValueHandlers(this.returnValueHandlers);
    ServletWebRequest webRequest = new ServletWebRequest(request, response);
    ModelAndViewContainer mavContainer = new ModelAndViewContainer();
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Invoking @ExceptionHandler method: " + exceptionHandlerMethod);
        }
        Throwable cause = exception.getCause();
        if (cause != null) {
            // Expose cause as provided argument as well
            exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, cause, handlerMethod);
        } else {
            // Otherwise, just the given exception as-is
            exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, handlerMethod);
        }
    } catch (Throwable invocationEx) {
        // probably an accident (e.g. failed assertion or the like).
        if (invocationEx != exception && logger.isWarnEnabled()) {
            logger.warn("Failed to invoke @ExceptionHandler method: " + 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();
            request = webRequest.getNativeRequest(HttpServletRequest.class);
            RequestContextUtils.getOutputFlashMap(request).putAll(flashAttributes);
        }
        return mav;
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RedirectAttributes(org.springframework.web.servlet.mvc.support.RedirectAttributes) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) HttpStatus(org.springframework.http.HttpStatus) ModelMap(org.springframework.ui.ModelMap) ModelAndView(org.springframework.web.servlet.ModelAndView) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest)

Example 37 with ModelMap

use of org.springframework.ui.ModelMap in project spring-framework by spring-projects.

the class HttpEntityMethodProcessor method saveFlashAttributes.

private void saveFlashAttributes(ModelAndViewContainer mav, NativeWebRequest request, String location) {
    mav.setRedirectModelScenario(true);
    ModelMap model = mav.getModel();
    if (model instanceof RedirectAttributes) {
        Map<String, ?> flashAttributes = ((RedirectAttributes) model).getFlashAttributes();
        if (!CollectionUtils.isEmpty(flashAttributes)) {
            HttpServletRequest req = request.getNativeRequest(HttpServletRequest.class);
            HttpServletResponse res = request.getNativeRequest(HttpServletResponse.class);
            RequestContextUtils.getOutputFlashMap(req).putAll(flashAttributes);
            RequestContextUtils.saveOutputFlashMap(location, req, res);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RedirectAttributes(org.springframework.web.servlet.mvc.support.RedirectAttributes) ModelMap(org.springframework.ui.ModelMap) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 38 with ModelMap

use of org.springframework.ui.ModelMap in project spring-framework by spring-projects.

the class ModelAndViewMethodReturnValueHandlerTests method handleRedirectAttributesWithViewName.

@Test
public void handleRedirectAttributesWithViewName() throws Exception {
    RedirectAttributesModelMap redirectAttributes = new RedirectAttributesModelMap();
    mavContainer.setRedirectModel(redirectAttributes);
    ModelAndView mav = new ModelAndView("redirect:viewName", "attrName", "attrValue");
    handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest);
    ModelMap model = mavContainer.getModel();
    assertEquals("redirect:viewName", mavContainer.getViewName());
    assertEquals("attrValue", model.get("attrName"));
    assertSame(redirectAttributes, model);
}
Also used : RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap) ModelMap(org.springframework.ui.ModelMap) ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap) Test(org.junit.Test)

Example 39 with ModelMap

use of org.springframework.ui.ModelMap in project spring-framework by spring-projects.

the class ModelAndViewMethodReturnValueHandlerTests method handleRedirectAttributesWithoutRedirect.

@Test
public void handleRedirectAttributesWithoutRedirect() throws Exception {
    RedirectAttributesModelMap redirectAttributes = new RedirectAttributesModelMap();
    mavContainer.setRedirectModel(redirectAttributes);
    ModelAndView mav = new ModelAndView();
    handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest);
    ModelMap model = mavContainer.getModel();
    assertEquals(null, mavContainer.getView());
    assertTrue(mavContainer.getModel().isEmpty());
    assertNotSame("RedirectAttributes should not be used if controller doesn't redirect", redirectAttributes, model);
}
Also used : RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap) ModelMap(org.springframework.ui.ModelMap) ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap) Test(org.junit.Test)

Example 40 with ModelMap

use of org.springframework.ui.ModelMap in project spring-framework by spring-projects.

the class ModelAndViewMethodReturnValueHandlerTests method handleRedirectWithIgnoreDefaultModel.

// SPR-14045
@Test
public void handleRedirectWithIgnoreDefaultModel() throws Exception {
    mavContainer.setIgnoreDefaultModelOnRedirect(true);
    RedirectView redirectView = new RedirectView();
    ModelAndView mav = new ModelAndView(redirectView, "name", "value");
    handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest);
    ModelMap model = mavContainer.getModel();
    assertSame(redirectView, mavContainer.getView());
    assertEquals(1, model.size());
    assertEquals("value", model.get("name"));
}
Also used : RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap) ModelMap(org.springframework.ui.ModelMap) RedirectView(org.springframework.web.servlet.view.RedirectView) ModelAndView(org.springframework.web.servlet.ModelAndView) Test(org.junit.Test)

Aggregations

ModelMap (org.springframework.ui.ModelMap)53 Test (org.junit.Test)23 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)23 ModelAndView (org.springframework.web.servlet.ModelAndView)11 RedirectAttributesModelMap (org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap)9 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)6 ModelAndView (org.springframework.web.portlet.ModelAndView)5 Map (java.util.Map)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 IOException (java.io.IOException)3 Date (java.util.Date)3 StudyBean (org.akaza.openclinica.bean.managestudy.StudyBean)3 HttpSessionRequiredException (org.springframework.web.HttpSessionRequiredException)3 NimbusClient (backtype.storm.utils.NimbusClient)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 PortletPreferences (javax.portlet.PortletPreferences)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2