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());
}
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;
}
}
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);
}
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);
}
Aggregations