001/* 002 * Copyright 2002-2015 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.validation.support; 018 019import java.util.Map; 020 021import org.springframework.ui.ExtendedModelMap; 022import org.springframework.validation.BindingResult; 023 024/** 025 * Subclass of {@link org.springframework.ui.ExtendedModelMap} that automatically removes 026 * a {@link org.springframework.validation.BindingResult} object if the corresponding 027 * target attribute gets replaced through regular {@link Map} operations. 028 * 029 * <p>This is the class exposed to handler methods by Spring MVC, typically consumed through 030 * a declaration of the {@link org.springframework.ui.Model} interface. There is no need to 031 * build it within user code; a plain {@link org.springframework.ui.ModelMap} or even a just 032 * a regular {@link Map} with String keys will be good enough to return a user model. 033 * 034 * @author Juergen Hoeller 035 * @since 2.5.6 036 * @see org.springframework.validation.BindingResult 037 */ 038@SuppressWarnings("serial") 039public class BindingAwareModelMap extends ExtendedModelMap { 040 041 @Override 042 public Object put(String key, Object value) { 043 removeBindingResultIfNecessary(key, value); 044 return super.put(key, value); 045 } 046 047 @Override 048 public void putAll(Map<? extends String, ?> map) { 049 for (Map.Entry<? extends String, ?> entry : map.entrySet()) { 050 removeBindingResultIfNecessary(entry.getKey(), entry.getValue()); 051 } 052 super.putAll(map); 053 } 054 055 private void removeBindingResultIfNecessary(Object key, Object value) { 056 if (key instanceof String) { 057 String attributeName = (String) key; 058 if (!attributeName.startsWith(BindingResult.MODEL_KEY_PREFIX)) { 059 String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + attributeName; 060 BindingResult bindingResult = (BindingResult) get(bindingResultKey); 061 if (bindingResult != null && bindingResult.getTarget() != value) { 062 remove(bindingResultKey); 063 } 064 } 065 } 066 } 067 068}