001/* 002 * Copyright 2002-2018 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.ConcurrentModel; 022import org.springframework.validation.BindingResult; 023 024/** 025 * Subclass of {@link ConcurrentModel} that automatically removes 026 * the {@link BindingResult} object when its corresponding 027 * target attribute is replaced through regular {@link Map} operations. 028 * 029 * <p>This is the class exposed to handler methods by Spring WebFlux, 030 * typically consumed through a declaration of the 031 * {@link org.springframework.ui.Model} interface as a parameter type. 032 * There is typically no need to create it within user code. 033 * If necessary a handler method can return a regular {@code java.util.Map}, 034 * likely a {@code java.util.ConcurrentMap}, for a pre-determined model. 035 * 036 * @author Rossen Stoyanchev 037 * @since 5.0 038 * @see BindingResult 039 */ 040@SuppressWarnings("serial") 041public class BindingAwareConcurrentModel extends ConcurrentModel { 042 043 @Override 044 public Object put(String key, Object value) { 045 removeBindingResultIfNecessary(key, value); 046 return super.put(key, value); 047 } 048 049 private void removeBindingResultIfNecessary(String key, Object value) { 050 if (!key.startsWith(BindingResult.MODEL_KEY_PREFIX)) { 051 String resultKey = BindingResult.MODEL_KEY_PREFIX + key; 052 BindingResult result = (BindingResult) get(resultKey); 053 if (result != null && result.getTarget() != value) { 054 remove(resultKey); 055 } 056 } 057 } 058 059}