Browse Source

ConcurrentModel ignores null value for put (also used by putAll)

Issue: SPR-17141

(cherry picked from commit b8b6367f9b)
pull/1946/head
Juergen Hoeller 6 years ago
parent
commit
a9305dbe8b
  1. 24
      spring-context/src/main/java/org/springframework/ui/ConcurrentModel.java
  2. 8
      spring-context/src/main/java/org/springframework/validation/support/BindingAwareConcurrentModel.java

24
spring-context/src/main/java/org/springframework/ui/ConcurrentModel.java

@ -65,6 +65,23 @@ public class ConcurrentModel extends ConcurrentHashMap<String, Object> implement @@ -65,6 +65,23 @@ public class ConcurrentModel extends ConcurrentHashMap<String, Object> 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<? extends String, ?> map) {
for (Map.Entry<? extends String, ?> 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<String, Object> implement @@ -73,12 +90,7 @@ public class ConcurrentModel extends ConcurrentHashMap<String, Object> 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;
}

8
spring-context/src/main/java/org/springframework/validation/support/BindingAwareConcurrentModel.java

@ -1,5 +1,5 @@ @@ -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 { @@ -46,12 +46,6 @@ public class BindingAwareConcurrentModel extends ConcurrentModel {
return super.put(key, value);
}
@Override
public void putAll(Map<? extends String, ?> 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;

Loading…
Cancel
Save