@Override public ExtendedModelMap addAttribute(Object attributeValue) { super.addAttribute(attributeValue); return this; }
/** * Construct a new {@code ModelMap} containing the supplied attribute. * Uses attribute name generation to generate the key for the supplied model * object. * @see #addAttribute(Object) */ public ModelMap(Object attributeValue) { addAttribute(attributeValue); }
@Override public ExtendedModelMap addAttribute(String attributeName, @Nullable Object attributeValue) { super.addAttribute(attributeName, attributeValue); return this; }
@Override public RedirectAttributes addFlashAttribute(String attributeName, @Nullable Object attributeValue) { this.flashAttributes.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 the supplied attribute to the underlying model. * A shortcut for {@code getModel().addAttribute(Object)}. */ public ModelAndViewContainer addAttribute(Object value) { getModel().addAttribute(value); return this; }
/** * {@inheritDoc} * <p>Formats the attribute value as a String before adding it. */ @Override public RedirectAttributesModelMap addAttribute(String attributeName, @Nullable Object attributeValue) { super.addAttribute(attributeName, formatValue(attributeValue)); return this; }
@RequestMapping(value = "/", method = RequestMethod.GET) public String sample(ModelMap model) { model.addAttribute("hello", "Hello World!"); return "index"; } }
@Test(expected = IllegalArgumentException.class) public void testAddObjectWithNull() throws Exception { // Null model arguments added without a name being explicitly supplied are not allowed ModelMap model = new ModelMap(); model.addAttribute(null); }
@Test public void testAopJdkProxyWithDetectedInterfaces() throws Exception { ModelMap map = new ModelMap(); Map<?, ?> target = new HashMap<>(); ProxyFactory factory = new ProxyFactory(target); Object proxy = factory.getProxy(); map.addAttribute(proxy); assertSame(proxy, map.get("map")); }
@Test public void testAddObjectNoKeyOfSameTypeOverrides() throws Exception { ModelMap model = new ModelMap(); model.addAttribute("foo"); model.addAttribute("bar"); assertEquals(1, model.size()); String bar = (String) model.get("string"); assertEquals("bar", bar); }
@RequestMapping("/myPath.do") public String myHandle(@ModelAttribute(name="myCommand", binding=true) TestBean tb, BindingResult errors, ModelMap model) { FieldError error = errors.getFieldError("age"); assertNotNull("Must have field error for age property", error); assertEquals("value2", error.getRejectedValue()); if (!model.containsKey("myKey")) { model.addAttribute("myKey", "myValue"); } return "myView"; } }
@RequestMapping("/myPath.do") public String myHandle(@ModelAttribute("myCommand") TestBean tb, BindingResult errors, ModelMap model) { FieldError error = errors.getFieldError("age"); assertNotNull("Must have field error for age property", error); assertEquals("value2", error.getRejectedValue()); if (!model.containsKey("myKey")) { model.addAttribute("myKey", "myValue"); } return "myView"; } }
@Test public void testAopJdkProxy() throws Exception { ModelMap map = new ModelMap(); ProxyFactory factory = new ProxyFactory(); Map<?, ?> target = new HashMap<>(); factory.setTarget(target); factory.addInterface(Map.class); Object proxy = factory.getProxy(); map.addAttribute(proxy); assertSame(proxy, map.get("map")); }
@Test public void noMatchBecauseNotSupported() throws Exception { this.view = new HttpMessageWriterView(new Jaxb2XmlEncoder()); this.view.setModelKeys(new HashSet<>(Collections.singletonList("foo1"))); this.model.addAttribute("foo1", "bar1"); assertEquals("", doRender()); }
@Test public void testInnerClassWithTwoUpperCaseLetters() throws Exception { ModelMap map = new ModelMap(); UKInnerClass inner = new UKInnerClass(); map.addAttribute(inner); assertSame(inner, map.get("UKInnerClass")); }
@Test public void testInnerClass() throws Exception { ModelMap map = new ModelMap(); SomeInnerClass inner = new SomeInnerClass(); map.addAttribute(inner); assertSame(inner, map.get("someInnerClass")); }
@Test public void testAddNullObjectWithExplicitKey() throws Exception { ModelMap model = new ModelMap(); model.addAttribute("foo", null); assertTrue(model.containsKey("foo")); assertNull(model.get("foo")); }
@Test public void testAopCglibProxy() throws Exception { ModelMap map = new ModelMap(); ProxyFactory factory = new ProxyFactory(); SomeInnerClass val = new SomeInnerClass(); factory.setTarget(val); factory.setProxyTargetClass(true); map.addAttribute(factory.getProxy()); assertTrue(map.containsKey("someInnerClass")); assertEquals(val, map.get("someInnerClass")); }
@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); }