From 5aed117b68bd937f1b9d9fad78d70669b7ab3f73 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 28 Jan 2019 22:50:29 +0100 Subject: [PATCH] Model interface exposes getAttribute method (next to add/contains) Fixes gh-22145 --- .../org/springframework/ui/ConcurrentModel.java | 8 +++++++- .../src/main/java/org/springframework/ui/Model.java | 11 ++++++++++- .../main/java/org/springframework/ui/ModelMap.java | 13 ++++++++++++- 3 files changed, 29 insertions(+), 3 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 4f52ecdf826..18b7fc0c7f9 100644 --- a/spring-context/src/main/java/org/springframework/ui/ConcurrentModel.java +++ b/spring-context/src/main/java/org/springframework/ui/ConcurrentModel.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -161,6 +161,12 @@ public class ConcurrentModel extends ConcurrentHashMap implement return containsKey(attributeName); } + @Override + @Nullable + public Object getAttribute(String attributeName) { + return get(attributeName); + } + @Override public Map asMap() { return this; diff --git a/spring-context/src/main/java/org/springframework/ui/Model.java b/spring-context/src/main/java/org/springframework/ui/Model.java index 1cf1d49a352..289a16db6b4 100644 --- a/spring-context/src/main/java/org/springframework/ui/Model.java +++ b/spring-context/src/main/java/org/springframework/ui/Model.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -76,6 +76,15 @@ public interface Model { */ boolean containsAttribute(String attributeName); + /** + * Return the attribute value for the given name, if any. + * @param attributeName the name of the model attribute (never {@code null}) + * @return the corresponding attribute value, or {@code null} if none + * @since 5.2 + */ + @Nullable + Object getAttribute(String attributeName); + /** * Return the current set of model attributes as a Map. */ diff --git a/spring-context/src/main/java/org/springframework/ui/ModelMap.java b/spring-context/src/main/java/org/springframework/ui/ModelMap.java index 3e37791ca24..f603c78517b 100644 --- a/spring-context/src/main/java/org/springframework/ui/ModelMap.java +++ b/spring-context/src/main/java/org/springframework/ui/ModelMap.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -144,4 +144,15 @@ public class ModelMap extends LinkedHashMap { return containsKey(attributeName); } + /** + * Return the attribute value for the given name, if any. + * @param attributeName the name of the model attribute (never {@code null}) + * @return the corresponding attribute value, or {@code null} if none + * @since 5.2 + */ + @Nullable + public Object getAttribute(String attributeName) { + return get(attributeName); + } + }