diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/AbstractTemplateView.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/AbstractTemplateView.java index 7a1a24cd756..c6050a5d2fe 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/AbstractTemplateView.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/AbstractTemplateView.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2023 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. @@ -66,6 +66,8 @@ public abstract class AbstractTemplateView extends AbstractUrlBasedView { /** * Set whether all request attributes should be added to the * model prior to merging with the template. Default is "false". + *
Note that some templates may make request attributes visible
+ * on their own, e.g. FreeMarker, without exposure in the MVC model.
*/
public void setExposeRequestAttributes(boolean exposeRequestAttributes) {
this.exposeRequestAttributes = exposeRequestAttributes;
@@ -73,7 +75,7 @@ public abstract class AbstractTemplateView extends AbstractUrlBasedView {
/**
* Set whether HttpServletRequest attributes are allowed to override (hide)
- * controller generated model attributes of the same name. Default is "false",
+ * controller generated model attributes of the same name. Default is "false"
* which causes an exception to be thrown if request attributes of the same
* name as model attributes are found.
*/
@@ -122,11 +124,11 @@ public abstract class AbstractTemplateView extends AbstractUrlBasedView {
String attribute = en.nextElement();
if (model.containsKey(attribute) && !this.allowRequestOverride) {
throw new ServletException("Cannot expose request attribute '" + attribute +
- "' because of an existing model object of the same name");
+ "' because of an existing model object of the same name");
}
Object attributeValue = request.getAttribute(attribute);
if (logger.isDebugEnabled()) {
- exposed = exposed != null ? exposed : new LinkedHashMap<>();
+ exposed = (exposed != null ? exposed : new LinkedHashMap<>());
exposed.put(attribute, attributeValue);
}
model.put(attribute, attributeValue);
@@ -144,11 +146,11 @@ public abstract class AbstractTemplateView extends AbstractUrlBasedView {
String attribute = en.nextElement();
if (model.containsKey(attribute) && !this.allowSessionOverride) {
throw new ServletException("Cannot expose session attribute '" + attribute +
- "' because of an existing model object of the same name");
+ "' because of an existing model object of the same name");
}
Object attributeValue = session.getAttribute(attribute);
if (logger.isDebugEnabled()) {
- exposed = exposed != null ? exposed : new LinkedHashMap<>();
+ exposed = (exposed != null ? exposed : new LinkedHashMap<>());
exposed.put(attribute, attributeValue);
}
model.put(attribute, attributeValue);
diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewTests.java
index d9fd65dce96..a7a79aa1659 100644
--- a/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewTests.java
+++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2019 the original author or authors.
+ * Copyright 2002-2023 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.
@@ -145,6 +145,34 @@ public class FreeMarkerViewTests {
assertThat(response.getContentType()).isEqualTo("myContentType");
}
+ @Test
+ public void requestAttributeVisible() throws Exception {
+ FreeMarkerView fv = new FreeMarkerView();
+
+ WebApplicationContext wac = mock(WebApplicationContext.class);
+ MockServletContext sc = new MockServletContext();
+
+ Map