/** * Copy attributes in the supplied {@code Map} with existing objects of * the same name taking precedence (i.e. not getting replaced). * A shortcut for {@code getModel().mergeAttributes(Map<String, ?>)}. */ public ModelAndViewContainer mergeAttributes(@Nullable Map<String, ?> attributes) { getModel().mergeAttributes(attributes); return this; }
/** * Remove the given attributes from the model. */ public ModelAndViewContainer removeAttributes(@Nullable Map<String, ?> attributes) { if (attributes != null) { for (String key : attributes.keySet()) { getModel().remove(key); } } return this; }
/** * Whether the underlying model contains the given attribute name. * A shortcut for {@code getModel().containsAttribute(String)}. */ public boolean containsAttribute(String name) { return getModel().containsAttribute(name); }
/** * Add the supplied attribute to the underlying model. * A shortcut for {@code getModel().addAttribute(String, Object)}. */ public ModelAndViewContainer addAttribute(String name, @Nullable Object value) { getModel().addAttribute(name, value); return this; }
@Test public void handleModelReturnValue() throws Exception { mavContainer.addAttribute("attr1", "value1"); Model returnValue = new ExtendedModelMap(); returnValue.addAttribute("attr2", "value2"); processor.handleReturnValue(returnValue , returnParamModel, mavContainer, webRequest); assertEquals("value1", mavContainer.getModel().get("attr1")); assertEquals("value2", mavContainer.getModel().get("attr2")); }
@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); }
@Test public void handleRedirectAttributesWithViewReference() throws Exception { RedirectAttributesModelMap redirectAttributes = new RedirectAttributesModelMap(); mavContainer.setRedirectModel(redirectAttributes); ModelAndView mav = new ModelAndView(new RedirectView(), "attrName", "attrValue"); handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest); assertEquals(RedirectView.class, mavContainer.getView().getClass()); assertEquals("attrValue", mavContainer.getModel().get("attrName")); assertSame("RedirectAttributes should be used if controller redirects", redirectAttributes, mavContainer.getModel()); }
@Test public void handleAnnotatedReturnValue() throws Exception { this.processor.handleReturnValue("expected", this.returnParamNamedModelAttr, this.container, this.request); assertEquals("expected", this.container.getModel().get("modelAttrName")); }
@Test // SPR-14045 public void ignoreDefaultModelAndWithoutRedirectModel() { this.mavContainer.setIgnoreDefaultModelOnRedirect(true); this.mavContainer.setRedirectModelScenario(true); this.mavContainer.addAttribute("name", "value"); assertEquals(1, this.mavContainer.getModel().size()); assertEquals("value", this.mavContainer.getModel().get("name")); }
@Test public void handleViewInstance() throws Exception { ModelAndView mav = new ModelAndView(new RedirectView(), "attrName", "attrValue"); handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest); assertEquals(RedirectView.class, mavContainer.getView().getClass()); assertEquals("attrValue", mavContainer.getModel().get("attrName")); }
@Test public void handleNotAnnotatedReturnValue() throws Exception { TestBean testBean = new TestBean("expected"); this.processor.handleReturnValue(testBean, this.returnParamNonSimpleType, this.container, this.request); assertSame(testBean, this.container.getModel().get("testBean")); }
@Test public void modelAttributeMethod() throws Exception { ModelFactory modelFactory = createModelFactory("modelAttr", Model.class); HandlerMethod handlerMethod = createHandlerMethod("handle"); modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod); assertEquals(Boolean.TRUE, this.mavContainer.getModel().get("modelAttr")); }
@Test public void modelAttributeMethodWithNameByConvention() throws Exception { ModelFactory modelFactory = createModelFactory("modelAttrConvention"); HandlerMethod handlerMethod = createHandlerMethod("handle"); modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod); assertEquals(Boolean.TRUE, this.mavContainer.getModel().get("boolean")); }
@Test public void modelAttributeMethodWithExplicitName() throws Exception { ModelFactory modelFactory = createModelFactory("modelAttrWithName"); HandlerMethod handlerMethod = createHandlerMethod("handle"); modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod); assertEquals(Boolean.TRUE, this.mavContainer.getModel().get("name")); }
@Test public void ignoreDefaultModel() { this.mavContainer.setIgnoreDefaultModelOnRedirect(true); this.mavContainer.addAttribute("name", "value"); this.mavContainer.setRedirectModelScenario(true); assertTrue(this.mavContainer.getModel().isEmpty()); }
@Test public void handleRedirectAttributesWithCustomPrefix() throws Exception { RedirectAttributesModelMap redirectAttributes = new RedirectAttributesModelMap(); mavContainer.setRedirectModel(redirectAttributes); ModelAndView mav = new ModelAndView("myRedirect:viewName", "attrName", "attrValue"); handler.setRedirectPatterns("myRedirect:*"); handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest); ModelMap model = mavContainer.getModel(); assertEquals("myRedirect:viewName", mavContainer.getViewName()); assertEquals("attrValue", model.get("attrName")); assertSame(redirectAttributes, model); }
@Test public void sessionAttribute() throws Exception { this.attributeStore.storeAttribute(this.webRequest, "sessionAttr", "sessionAttrValue"); ModelFactory modelFactory = createModelFactory("modelAttr", Model.class); HandlerMethod handlerMethod = createHandlerMethod("handle"); modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod); assertEquals("sessionAttrValue", this.mavContainer.getModel().get("sessionAttr")); }
@Test // SPR-14045 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")); }
@Test(expected = BindException.class) public void resolveArgumentBindException() throws Exception { String name = "testBean"; Object target = new TestBean(); this.container.getModel().addAttribute(target); StubRequestDataBinder dataBinder = new StubRequestDataBinder(target, name); dataBinder.getBindingResult().reject("error"); WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(this.request, target, name)).willReturn(dataBinder); this.processor.resolveArgument(this.paramNonSimpleType, this.container, this.request, binderFactory); verify(binderFactory).createBinder(this.request, target, name); }