/** * Return the model map. Never returns {@code null}. * To be called by application code for modifying the model. */ public Map<String, Object> getModel() { return getModelMap(); }
/** * Return the model map. Never returns {@code null}. * To be called by application code for modifying the model. */ public Map<String, Object> getModel() { return getModelMap(); }
/** * Add an attribute to the model. * @param attributeName name of the object to add to the model (never {@code null}) * @param attributeValue object to add to the model (can be {@code null}) * @see ModelMap#addAttribute(String, Object) * @see #getModelMap() */ public ModelAndView addObject(String attributeName, @Nullable Object attributeValue) { getModelMap().addAttribute(attributeName, attributeValue); return this; }
/** * Add an attribute to the model using parameter name generation. * @param attributeValue the object to add to the model (never {@code null}) * @see ModelMap#addAttribute(Object) * @see #getModelMap() */ public ModelAndView addObject(Object attributeValue) { getModelMap().addAttribute(attributeValue); return this; }
/** * Add all attributes contained in the provided Map to the model. * @param modelMap a Map of attributeName -> attributeValue pairs * @see ModelMap#addAllAttributes(Map) * @see #getModelMap() */ public ModelAndView addAllObjects(@Nullable Map<String, ?> modelMap) { getModelMap().addAllAttributes(modelMap); return this; }
/** * Create a new ModelAndView given a view name and a model. * @param viewName name of the View to render, to be resolved * by the DispatcherServlet's ViewResolver * @param model a Map of model names (Strings) to model objects * (Objects). Model entries may not be {@code null}, but the * model Map may be {@code null} if there is no model data. */ public ModelAndView(String viewName, @Nullable Map<String, ?> model) { this.view = viewName; if (model != null) { getModelMap().addAllAttributes(model); } }
/** * Create a new ModelAndView given a View object and a model. * <em>Note: the supplied model data is copied into the internal * storage of this class. You should not consider to modify the supplied * Map after supplying it to this class</em> * @param view the View object to render * @param model a Map of model names (Strings) to model objects * (Objects). Model entries may not be {@code null}, but the * model Map may be {@code null} if there is no model data. */ public ModelAndView(View view, @Nullable Map<String, ?> model) { this.view = view; if (model != null) { getModelMap().addAllAttributes(model); } }
/** * Create a new ModelAndView given a view name, model, and HTTP status. * @param viewName name of the View to render, to be resolved * by the DispatcherServlet's ViewResolver * @param model a Map of model names (Strings) to model objects * (Objects). Model entries may not be {@code null}, but the * model Map may be {@code null} if there is no model data. * @param status an HTTP status code to use for the response * (to be set just prior to View rendering) * @since 4.3 */ public ModelAndView(@Nullable String viewName, @Nullable Map<String, ?> model, @Nullable HttpStatus status) { this.view = viewName; if (model != null) { getModelMap().addAllAttributes(model); } this.status = status; }
/** * Add an attribute to the model using parameter name generation. * @param attributeValue the object to add to the model (never {@code null}) * @see ModelMap#addAttribute(Object) * @see #getModelMap() */ public ModelAndView addObject(Object attributeValue) { getModelMap().addAttribute(attributeValue); return this; }
/** * Add all attributes contained in the provided Map to the model. * @param modelMap a Map of attributeName -> attributeValue pairs * @see ModelMap#addAllAttributes(Map) * @see #getModelMap() */ public ModelAndView addAllObjects(@Nullable Map<String, ?> modelMap) { getModelMap().addAllAttributes(modelMap); return this; }
/** * Add an attribute to the model. * @param attributeName name of the object to add to the model (never {@code null}) * @param attributeValue object to add to the model (can be {@code null}) * @see ModelMap#addAttribute(String, Object) * @see #getModelMap() */ public ModelAndView addObject(String attributeName, @Nullable Object attributeValue) { getModelMap().addAttribute(attributeName, attributeValue); return this; }
@Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception { this.requestInterceptor.postHandle(new DispatcherServletWebRequest(request, response), (modelAndView != null && !modelAndView.wasCleared() ? modelAndView.getModelMap() : null)); }
/** * Create a new ModelAndView given a view name and a model. * @param viewName name of the View to render, to be resolved * by the DispatcherServlet's ViewResolver * @param model a Map of model names (Strings) to model objects * (Objects). Model entries may not be {@code null}, but the * model Map may be {@code null} if there is no model data. */ public ModelAndView(String viewName, @Nullable Map<String, ?> model) { this.view = viewName; if (model != null) { getModelMap().addAllAttributes(model); } }
/** * Assert the total number of errors in the model. */ public <T> ResultMatcher errorCount(final int expectedCount) { return result -> { int actualCount = getErrorCount(getModelAndView(result).getModelMap()); assertEquals("Binding/validation error count", expectedCount, actualCount); }; }
/** * Assert the model has errors. */ public <T> ResultMatcher hasErrors() { return result -> { int count = getErrorCount(getModelAndView(result).getModelMap()); assertTrue("Expected binding/validation errors", count != 0); }; }
@Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception { this.requestInterceptor.postHandle(new DispatcherServletWebRequest(request, response), (modelAndView != null && !modelAndView.wasCleared() ? modelAndView.getModelMap() : null)); }
@Test // SPR-13546 public void resolveExceptionModelAtArgument() throws Exception { IllegalArgumentException ex = new IllegalArgumentException(); HandlerMethod handlerMethod = new HandlerMethod(new ModelArgumentController(), "handle"); this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull(mav); assertEquals(1, mav.getModelMap().size()); assertEquals("IllegalArgumentException", mav.getModelMap().get("exceptionClassName")); }
@Test public void handleRequestPart() throws Exception { MockMultipartHttpServletRequest multipartRequest = new MockMultipartHttpServletRequest(); multipartRequest.addFile(new MockMultipartFile("requestPart", "", "text/plain", "content".getBytes("UTF-8"))); HandlerMethod handlerMethod = handlerMethod("handleRequestPart", String.class, Model.class); ModelAndView mav = handlerAdapter.handle(multipartRequest, response, handlerMethod); assertNotNull(mav); assertEquals("content", mav.getModelMap().get("requestPart")); }
@Test public void handleAndValidateRequestPart() throws Exception { MockMultipartHttpServletRequest multipartRequest = new MockMultipartHttpServletRequest(); multipartRequest.addFile(new MockMultipartFile("requestPart", "", "text/plain", "content".getBytes("UTF-8"))); HandlerMethod handlerMethod = handlerMethod("handleAndValidateRequestPart", String.class, Errors.class, Model.class); ModelAndView mav = handlerAdapter.handle(multipartRequest, response, handlerMethod); assertNotNull(mav); assertEquals(1, mav.getModelMap().get("error count")); }
ModelMap model = mav.getModelMap();