Add ViewResolverRegistry#scriptTemplate in WebFlux

Issue: SPR-16431
This commit is contained in:
sdeleuze 2018-01-31 10:37:02 +01:00
parent c7f60d1799
commit b2681e1f4a
2 changed files with 55 additions and 2 deletions

View File

@ -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.
* <p><strong>Note</strong> 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();
}
}
}

View File

@ -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<ViewResolver> viewResolvers = this.registry.getViewResolvers();
assertEquals(1, viewResolvers.size());
assertEquals(ScriptTemplateViewResolver.class, viewResolvers.get(0).getClass());
DirectFieldAccessor accessor = new DirectFieldAccessor(viewResolvers.get(0));
assertEquals("/", accessor.getPropertyValue("prefix"));
assertEquals(".html", accessor.getPropertyValue("suffix"));
}
}