Java Code Examples for org.springframework.validation.BindingResult#MODEL_KEY_PREFIX

The following examples show how to use org.springframework.validation.BindingResult#MODEL_KEY_PREFIX . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModelFactory.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Add {@link BindingResult} attributes to the model for attributes that require it.
 */
private void updateBindingResult(NativeWebRequest request, ModelMap model) throws Exception {
	List<String> keyNames = new ArrayList<String>(model.keySet());
	for (String name : keyNames) {
		Object value = model.get(name);

		if (isBindingCandidate(name, value)) {
			String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + name;

			if (!model.containsAttribute(bindingResultKey)) {
				WebDataBinder dataBinder = dataBinderFactory.createBinder(request, value, name);
				model.put(bindingResultKey, dataBinder.getBindingResult());
			}
		}
	}
}
 
Example 2
Source File: ModelAttributeMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void testBindFoo(String modelKey, MethodParameter param, Function<Object, Foo> valueExtractor)
		throws Exception {

	Object value = createResolver()
			.resolveArgument(param, this.bindContext, postForm("name=Robert&age=25"))
			.block(Duration.ZERO);

	Foo foo = valueExtractor.apply(value);
	assertEquals("Robert", foo.getName());
	assertEquals(25, foo.getAge());

	String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + modelKey;

	Map<String, Object> map = bindContext.getModel().asMap();
	assertEquals(map.toString(), 2, map.size());
	assertSame(foo, map.get(modelKey));
	assertNotNull(map.get(bindingResultKey));
	assertTrue(map.get(bindingResultKey) instanceof BindingResult);
}
 
Example 3
Source File: ModelAttributeMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void testBindBar(MethodParameter param) throws Exception {
	Object value = createResolver()
			.resolveArgument(param, this.bindContext, postForm("name=Robert&age=25&count=1"))
			.block(Duration.ZERO);

	Bar bar = (Bar) value;
	assertEquals("Robert", bar.getName());
	assertEquals(25, bar.getAge());
	assertEquals(1, bar.getCount());

	String key = "bar";
	String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + key;

	Map<String, Object> map = bindContext.getModel().asMap();
	assertEquals(map.toString(), 2, map.size());
	assertSame(bar, map.get(key));
	assertNotNull(map.get(bindingResultKey));
	assertTrue(map.get(bindingResultKey) instanceof BindingResult);
}
 
Example 4
Source File: ModelFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void updateModelBindingResult() throws Exception {
	String commandName = "attr1";
	Object command = new Object();
	ModelAndViewContainer container = new ModelAndViewContainer();
	container.addAttribute(commandName, command);

	WebDataBinder dataBinder = new WebDataBinder(command, commandName);
	WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
	given(binderFactory.createBinder(this.webRequest, command, commandName)).willReturn(dataBinder);

	ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler);
	modelFactory.updateModel(this.webRequest, container);

	assertEquals(command, container.getModel().get(commandName));
	String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + commandName;
	assertSame(dataBinder.getBindingResult(), container.getModel().get(bindingResultKey));
	assertEquals(2, container.getModel().size());
}
 
Example 5
Source File: SetModelAndViewInterceptor.java    From spring-boot-doma2-sample with Apache License 2.0 6 votes vote down vote up
/**
 * 入力エラーを画面オブジェクトに設定する
 * 
 * @param modelAndView
 */
protected void retainValidateErrors(ModelAndView modelAndView) {
    val model = modelAndView.getModelMap();

    if (model != null && model.containsAttribute(MAV_ERRORS)) {
        val errors = model.get(MAV_ERRORS);

        if (errors != null && errors instanceof BeanPropertyBindingResult) {
            val br = ((BeanPropertyBindingResult) errors);

            if (br.hasErrors()) {
                val formName = br.getObjectName();
                val key = BindingResult.MODEL_KEY_PREFIX + formName;
                model.addAttribute(key, errors);
                model.addAttribute(GLOBAL_MESSAGE, MessageUtils.getMessage(VALIDATION_ERROR));
            }
        }
    }
}
 
Example 6
Source File: ModelAttributeMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void testBindFoo(String modelKey, MethodParameter param, Function<Object, Foo> valueExtractor)
		throws Exception {

	Object value = createResolver()
			.resolveArgument(param, this.bindContext, postForm("name=Robert&age=25"))
			.block(Duration.ZERO);

	Foo foo = valueExtractor.apply(value);
	assertEquals("Robert", foo.getName());
	assertEquals(25, foo.getAge());

	String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + modelKey;

	Map<String, Object> map = bindContext.getModel().asMap();
	assertEquals(map.toString(), 2, map.size());
	assertSame(foo, map.get(modelKey));
	assertNotNull(map.get(bindingResultKey));
	assertTrue(map.get(bindingResultKey) instanceof BindingResult);
}
 
Example 7
Source File: ModelAttributeMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void testBindBar(MethodParameter param) throws Exception {
	Object value = createResolver()
			.resolveArgument(param, this.bindContext, postForm("name=Robert&age=25&count=1"))
			.block(Duration.ZERO);

	Bar bar = (Bar) value;
	assertEquals("Robert", bar.getName());
	assertEquals(25, bar.getAge());
	assertEquals(1, bar.getCount());

	String key = "bar";
	String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + key;

	Map<String, Object> map = bindContext.getModel().asMap();
	assertEquals(map.toString(), 2, map.size());
	assertSame(bar, map.get(key));
	assertNotNull(map.get(bindingResultKey));
	assertTrue(map.get(bindingResultKey) instanceof BindingResult);
}
 
Example 8
Source File: ModelFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void updateModelBindingResult() throws Exception {
	String commandName = "attr1";
	Object command = new Object();
	ModelAndViewContainer container = new ModelAndViewContainer();
	container.addAttribute(commandName, command);

	WebDataBinder dataBinder = new WebDataBinder(command, commandName);
	WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
	given(binderFactory.createBinder(this.webRequest, command, commandName)).willReturn(dataBinder);

	ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler);
	modelFactory.updateModel(this.webRequest, container);

	assertEquals(command, container.getModel().get(commandName));
	String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + commandName;
	assertSame(dataBinder.getBindingResult(), container.getModel().get(bindingResultKey));
	assertEquals(2, container.getModel().size());
}
 
Example 9
Source File: BindingAwareConcurrentModel.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void removeBindingResultIfNecessary(String key, Object value) {
	if (!key.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
		String resultKey = BindingResult.MODEL_KEY_PREFIX + key;
		BindingResult result = (BindingResult) get(resultKey);
		if (result != null && result.getTarget() != value) {
			remove(resultKey);
		}
	}
}
 
Example 10
Source File: BindingAwareModelMap.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void removeBindingResultIfNecessary(Object key, Object value) {
	if (key instanceof String) {
		String attributeName = (String) key;
		if (!attributeName.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
			String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + attributeName;
			BindingResult bindingResult = (BindingResult) get(bindingResultKey);
			if (bindingResult != null && bindingResult.getTarget() != value) {
				remove(bindingResultKey);
			}
		}
	}
}
 
Example 11
Source File: ModelFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Add {@link BindingResult} attributes to the model for attributes that require it.
 */
private void updateBindingResult(NativeWebRequest request, ModelMap model) throws Exception {
	List<String> keyNames = new ArrayList<String>(model.keySet());
	for (String name : keyNames) {
		Object value = model.get(name);
		if (isBindingCandidate(name, value)) {
			String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + name;
			if (!model.containsAttribute(bindingResultKey)) {
				WebDataBinder dataBinder = this.dataBinderFactory.createBinder(request, value, name);
				model.put(bindingResultKey, dataBinder.getBindingResult());
			}
		}
	}
}
 
Example 12
Source File: BindingAwareModelMap.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void removeBindingResultIfNecessary(Object key, Object value) {
	if (key instanceof String) {
		String attributeName = (String) key;
		if (!attributeName.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
			String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + attributeName;
			BindingResult bindingResult = (BindingResult) get(bindingResultKey);
			if (bindingResult != null && bindingResult.getTarget() != value) {
				remove(bindingResultKey);
			}
		}
	}
}
 
Example 13
Source File: ModelFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Add {@link BindingResult} attributes to the model for attributes that require it.
 */
private void updateBindingResult(NativeWebRequest request, ModelMap model) throws Exception {
	List<String> keyNames = new ArrayList<>(model.keySet());
	for (String name : keyNames) {
		Object value = model.get(name);
		if (value != null && isBindingCandidate(name, value)) {
			String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + name;
			if (!model.containsAttribute(bindingResultKey)) {
				WebDataBinder dataBinder = this.dataBinderFactory.createBinder(request, value, name);
				model.put(bindingResultKey, dataBinder.getBindingResult());
			}
		}
	}
}
 
Example 14
Source File: BindingAwareConcurrentModel.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void removeBindingResultIfNecessary(String key, Object value) {
	if (!key.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
		String resultKey = BindingResult.MODEL_KEY_PREFIX + key;
		BindingResult result = (BindingResult) get(resultKey);
		if (result != null && result.getTarget() != value) {
			remove(resultKey);
		}
	}
}
 
Example 15
Source File: BindingAwareModelMap.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void removeBindingResultIfNecessary(Object key, Object value) {
	if (key instanceof String) {
		String attributeName = (String) key;
		if (!attributeName.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
			String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + attributeName;
			BindingResult bindingResult = (BindingResult) get(bindingResultKey);
			if (bindingResult != null && bindingResult.getTarget() != value) {
				remove(bindingResultKey);
			}
		}
	}
}
 
Example 16
Source File: ModelFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Add {@link BindingResult} attributes to the model for attributes that require it.
 */
private void updateBindingResult(NativeWebRequest request, ModelMap model) throws Exception {
	List<String> keyNames = new ArrayList<>(model.keySet());
	for (String name : keyNames) {
		Object value = model.get(name);
		if (value != null && isBindingCandidate(name, value)) {
			String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + name;
			if (!model.containsAttribute(bindingResultKey)) {
				WebDataBinder dataBinder = this.dataBinderFactory.createBinder(request, value, name);
				model.put(bindingResultKey, dataBinder.getBindingResult());
			}
		}
	}
}
 
Example 17
Source File: BindingAwareModelMap.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void removeBindingResultIfNecessary(Object key, Object value) {
	if (key instanceof String) {
		String attributeName = (String) key;
		if (!attributeName.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
			String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + attributeName;
			BindingResult bindingResult = (BindingResult) get(bindingResultKey);
			if (bindingResult != null && bindingResult.getTarget() != value) {
				remove(bindingResultKey);
			}
		}
	}
}
 
Example 18
Source File: ModelFactoryTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private String bindingResultKey(String key) {
	return BindingResult.MODEL_KEY_PREFIX + key;
}