diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/config/ViewResolverRegistry.java b/spring-webflux/src/main/java/org/springframework/web/reactive/config/ViewResolverRegistry.java index 8c187d518f0..cceecc09304 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/config/ViewResolverRegistry.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/config/ViewResolverRegistry.java @@ -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. @@ -32,6 +32,8 @@ import org.springframework.web.reactive.result.view.View; import org.springframework.web.reactive.result.view.ViewResolver; import org.springframework.web.reactive.result.view.freemarker.FreeMarkerConfigurer; import org.springframework.web.reactive.result.view.freemarker.FreeMarkerViewResolver; +import org.springframework.web.reactive.result.view.script.ScriptTemplateConfigurer; +import org.springframework.web.reactive.result.view.script.ScriptTemplateViewResolver; /** * Assist with the configuration of a chain of {@link ViewResolver}'s supporting @@ -42,6 +44,7 @@ import org.springframework.web.reactive.result.view.freemarker.FreeMarkerViewRes * JSON, XML, etc. * * @author Rossen Stoyanchev + * @author Sebastien Deleuze * @since 5.0 */ public class ViewResolverRegistry { @@ -83,6 +86,28 @@ public class ViewResolverRegistry { return registration; } + /** + * Register a script template view resolver with an empty default view name prefix and suffix. + *
Note that you must also configure script templating by
+ * adding a {@link ScriptTemplateConfigurer} bean.
+ * @since 5.0.4
+ */
+ public UrlBasedViewResolverRegistration scriptTemplate() {
+ if (!checkBeanOfType(ScriptTemplateConfigurer.class)) {
+ throw new BeanInitializationException("In addition to a script template view resolver " +
+ "there must also be a single ScriptTemplateConfig bean in this web application context " +
+ "(or its parent): ScriptTemplateConfigurer is the usual implementation. " +
+ "This bean may be given any name.");
+ }
+ ScriptRegistration registration = new ScriptRegistration();
+ UrlBasedViewResolver resolver = registration.getViewResolver();
+ if (this.applicationContext != null) {
+ resolver.setApplicationContext(this.applicationContext);
+ }
+ this.viewResolvers.add(resolver);
+ return registration;
+ }
+
/**
* Register a {@link ViewResolver} bean instance. This may be useful to
* configure a 3rd party resolver implementation or as an alternative to
@@ -150,4 +175,12 @@ public class ViewResolverRegistry {
}
}
+ private static class ScriptRegistration extends UrlBasedViewResolverRegistration {
+
+ public ScriptRegistration() {
+ super(new ScriptTemplateViewResolver());
+ getViewResolver();
+ }
+ }
+
}
diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/config/ViewResolverRegistryTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/config/ViewResolverRegistryTests.java
index e7a0d82b408..6e477e1d1e4 100644
--- a/spring-webflux/src/test/java/org/springframework/web/reactive/config/ViewResolverRegistryTests.java
+++ b/spring-webflux/src/test/java/org/springframework/web/reactive/config/ViewResolverRegistryTests.java
@@ -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.
@@ -15,16 +15,22 @@
*/
package org.springframework.web.reactive.config;
+import java.util.List;
+
import org.junit.Before;
import org.junit.Test;
+import org.springframework.beans.DirectFieldAccessor;
import org.springframework.core.Ordered;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.reactive.result.view.HttpMessageWriterView;
import org.springframework.web.reactive.result.view.UrlBasedViewResolver;
import org.springframework.web.reactive.result.view.View;
+import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.reactive.result.view.freemarker.FreeMarkerConfigurer;
+import org.springframework.web.reactive.result.view.script.ScriptTemplateConfigurer;
+import org.springframework.web.reactive.result.view.script.ScriptTemplateViewResolver;
import static org.junit.Assert.*;
@@ -32,6 +38,7 @@ import static org.junit.Assert.*;
* Unit tests for {@link ViewResolverRegistry}.
*
* @author Rossen Stoyanchev
+ * @author Sebastien Deleuze
*/
public class ViewResolverRegistryTests {
@@ -42,6 +49,7 @@ public class ViewResolverRegistryTests {
public void setup() {
StaticWebApplicationContext context = new StaticWebApplicationContext();
context.registerSingleton("freeMarkerConfigurer", FreeMarkerConfigurer.class);
+ context.registerSingleton("scriptTemplateConfigurer", ScriptTemplateConfigurer.class);
this.registry = new ViewResolverRegistry(context);
}
@@ -84,4 +92,16 @@ public class ViewResolverRegistryTests {
assertSame(view, this.registry.getDefaultViews().get(0));
}
+ @Test // SPR-16431
+ public void scriptTemplate() {
+ this.registry.scriptTemplate().prefix("/").suffix(".html");
+
+ List