Skip to content

Commit 5aed117

Browse files
committed
Model interface exposes getAttribute method (next to add/contains)
Fixes gh-22145
1 parent 80385ce commit 5aed117

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -161,6 +161,12 @@ public boolean containsAttribute(String attributeName) {
161161
return containsKey(attributeName);
162162
}
163163

164+
@Override
165+
@Nullable
166+
public Object getAttribute(String attributeName) {
167+
return get(attributeName);
168+
}
169+
164170
@Override
165171
public Map<String, Object> asMap() {
166172
return this;

spring-context/src/main/java/org/springframework/ui/Model.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -76,6 +76,15 @@ public interface Model {
7676
*/
7777
boolean containsAttribute(String attributeName);
7878

79+
/**
80+
* Return the attribute value for the given name, if any.
81+
* @param attributeName the name of the model attribute (never {@code null})
82+
* @return the corresponding attribute value, or {@code null} if none
83+
* @since 5.2
84+
*/
85+
@Nullable
86+
Object getAttribute(String attributeName);
87+
7988
/**
8089
* Return the current set of model attributes as a Map.
8190
*/

spring-context/src/main/java/org/springframework/ui/ModelMap.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -144,4 +144,15 @@ public boolean containsAttribute(String attributeName) {
144144
return containsKey(attributeName);
145145
}
146146

147+
/**
148+
* Return the attribute value for the given name, if any.
149+
* @param attributeName the name of the model attribute (never {@code null})
150+
* @return the corresponding attribute value, or {@code null} if none
151+
* @since 5.2
152+
*/
153+
@Nullable
154+
public Object getAttribute(String attributeName) {
155+
return get(attributeName);
156+
}
157+
147158
}

0 commit comments

Comments
 (0)