diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerView.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerView.java
index 52a0c4e3a4..f2b5cb3ef8 100644
--- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerView.java
+++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerView.java
@@ -21,6 +21,7 @@ import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
+import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
@@ -47,6 +48,7 @@ import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.MimeType;
import org.springframework.web.reactive.result.view.AbstractUrlBasedView;
+import org.springframework.web.reactive.result.view.RequestContext;
import org.springframework.web.server.ServerWebExchange;
/**
@@ -64,16 +66,30 @@ import org.springframework.web.server.ServerWebExchange;
*
Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher.
*
* @author Rossen Stoyanchev
+ * @author Sam Brannen
* @since 5.0
*/
public class FreeMarkerView extends AbstractUrlBasedView {
+ /**
+ * Attribute name of the {@link RequestContext} instance in the template model,
+ * available to Spring's macros — for example, for creating
+ * {@link org.springframework.web.reactive.result.view.BindStatus BindStatus}
+ * objects.
+ * @since 5.2
+ * @see #setExposeSpringMacroHelpers(boolean)
+ */
+ public static final String SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE = "springMacroRequestContext";
+
+
@Nullable
private Configuration configuration;
@Nullable
private String encoding;
+ private boolean exposeSpringMacroHelpers = true;
+
/**
* Set the FreeMarker Configuration to be used by this view.
@@ -124,6 +140,19 @@ public class FreeMarkerView extends AbstractUrlBasedView {
return this.encoding;
}
+ /**
+ * Set whether to expose a {@link RequestContext} for use by Spring's macro
+ * library, under the name {@value #SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE}.
+ *
Default is {@code true}.
+ *
Needed for Spring's FreeMarker default macros. Note that this is
+ * not required for templates that use HTML forms unless you
+ * wish to take advantage of the Spring helper macros.
+ * @see #SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE
+ */
+ public void setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers) {
+ this.exposeSpringMacroHelpers = exposeSpringMacroHelpers;
+ }
+
@Override
public void afterPropertiesSet() throws Exception {
@@ -180,6 +209,34 @@ public class FreeMarkerView extends AbstractUrlBasedView {
}
}
+ /**
+ * Prepare the model to use for rendering by potentially exposing a
+ * {@link RequestContext} for use in Spring FreeMarker macros and then
+ * delegating to the inherited implementation of this method.
+ * @since 5.2
+ * @see #setExposeSpringMacroHelpers(boolean)
+ * @see org.springframework.web.reactive.result.view.AbstractView#getModelAttributes(Map, ServerWebExchange)
+ */
+ @Override
+ protected Mono