From a9305dbe8bbc01260928087f9eb570cfb1fd8355 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 8 Aug 2018 23:51:55 +0200 Subject: [PATCH] ConcurrentModel ignores null value for put (also used by putAll) Issue: SPR-17141 (cherry picked from commit b8b6367f9b22e24f90b758cc9e49fac97b58db89) --- .../springframework/ui/ConcurrentModel.java | 24 ++++++++++++++----- .../support/BindingAwareConcurrentModel.java | 8 +------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/ui/ConcurrentModel.java b/spring-context/src/main/java/org/springframework/ui/ConcurrentModel.java index 77e7651938..4f52ecdf82 100644 --- a/spring-context/src/main/java/org/springframework/ui/ConcurrentModel.java +++ b/spring-context/src/main/java/org/springframework/ui/ConcurrentModel.java @@ -65,6 +65,23 @@ public class ConcurrentModel extends ConcurrentHashMap implement } + @Override + public Object put(String key, Object value) { + if (value != null) { + return super.put(key, value); + } + else { + return remove(key); + } + } + + @Override + public void putAll(Map map) { + for (Map.Entry entry : map.entrySet()) { + put(entry.getKey(), entry.getValue()); + } + } + /** * Add the supplied attribute under the supplied name. * @param attributeName the name of the model attribute (never {@code null}) @@ -73,12 +90,7 @@ public class ConcurrentModel extends ConcurrentHashMap implement */ public ConcurrentModel addAttribute(String attributeName, @Nullable Object attributeValue) { Assert.notNull(attributeName, "Model attribute name must not be null"); - if (attributeValue != null) { - put(attributeName, attributeValue); - } - else { - remove(attributeName); - } + put(attributeName, attributeValue); return this; } diff --git a/spring-context/src/main/java/org/springframework/validation/support/BindingAwareConcurrentModel.java b/spring-context/src/main/java/org/springframework/validation/support/BindingAwareConcurrentModel.java index caf52d03eb..6d093e5046 100644 --- a/spring-context/src/main/java/org/springframework/validation/support/BindingAwareConcurrentModel.java +++ b/spring-context/src/main/java/org/springframework/validation/support/BindingAwareConcurrentModel.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,12 +46,6 @@ public class BindingAwareConcurrentModel extends ConcurrentModel { return super.put(key, value); } - @Override - public void putAll(Map map) { - map.forEach(this::removeBindingResultIfNecessary); - super.putAll(map); - } - private void removeBindingResultIfNecessary(String key, Object value) { if (!key.startsWith(BindingResult.MODEL_KEY_PREFIX)) { String resultKey = BindingResult.MODEL_KEY_PREFIX + key;