pull/9042/head
Phillip Webb 8 years ago
parent 1ee9653f7c
commit 72813a47e7

@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.thymeleaf;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import javax.annotation.PostConstruct;
@ -54,7 +55,6 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MimeType;
import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter;
@ -71,7 +71,7 @@ import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter;
@Configuration
@EnableConfigurationProperties(ThymeleafProperties.class)
@ConditionalOnClass(TemplateMode.class)
@AutoConfigureAfter({WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class})
@AutoConfigureAfter({ WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class })
public class ThymeleafAutoConfiguration {
@Configuration
@ -137,21 +137,16 @@ public class ThymeleafAutoConfiguration {
Collection<ITemplateResolver> templateResolvers,
ObjectProvider<Collection<IDialect>> dialectsProvider) {
this.templateResolvers = templateResolvers;
this.dialects = dialectsProvider.getIfAvailable();
this.dialects = dialectsProvider
.getIfAvailable(() -> Collections.emptyList());
}
@Bean
@ConditionalOnMissingBean(SpringTemplateEngine.class)
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
for (ITemplateResolver templateResolver : this.templateResolvers) {
engine.addTemplateResolver(templateResolver);
}
if (!CollectionUtils.isEmpty(this.dialects)) {
for (IDialect dialect : this.dialects) {
engine.addDialect(dialect);
}
}
this.templateResolvers.forEach(engine::addTemplateResolver);
this.dialects.forEach(engine::addDialect);
return engine;
}
@ -188,8 +183,9 @@ public class ThymeleafAutoConfiguration {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(this.templateEngine);
resolver.setCharacterEncoding(this.properties.getEncoding().name());
resolver.setContentType(appendCharset(this.properties.getServlet().getContentType(),
resolver.getCharacterEncoding()));
resolver.setContentType(
appendCharset(this.properties.getServlet().getContentType(),
resolver.getCharacterEncoding()));
resolver.setExcludedViewNames(this.properties.getExcludedViewNames());
resolver.setViewNames(this.properties.getViewNames());
// This resolver acts as a fallback resolver (e.g. like a
@ -222,27 +218,22 @@ public class ThymeleafAutoConfiguration {
private final Collection<IDialect> dialects;
ThymeleafReactiveConfiguration(Collection<ITemplateResolver> templateResolvers,
ObjectProvider<Collection<IDialect>> dialectsProvider) {
this.templateResolvers = templateResolvers;
this.dialects = dialectsProvider.getIfAvailable();
this.dialects = dialectsProvider
.getIfAvailable(() -> Collections.emptyList());
}
@Bean
@ConditionalOnMissingBean(ISpringWebFluxTemplateEngine.class)
public SpringWebFluxTemplateEngine templateEngine() {
SpringWebFluxTemplateEngine engine = new SpringWebFluxTemplateEngine();
for (ITemplateResolver templateResolver : this.templateResolvers) {
engine.addTemplateResolver(templateResolver);
}
if (!CollectionUtils.isEmpty(this.dialects)) {
for (IDialect dialect : this.dialects) {
engine.addDialect(dialect);
}
}
this.templateResolvers.forEach(engine::addTemplateResolver);
this.dialects.forEach(engine::addDialect);
return engine;
}
}
@Configuration
@ -252,23 +243,24 @@ public class ThymeleafAutoConfiguration {
private final ThymeleafProperties properties;
ThymeleafWebFluxConfiguration(ThymeleafProperties properties) {
this.properties = properties;
}
@Bean
@ConditionalOnMissingBean(name = "thymeleafReactiveViewResolver")
public ThymeleafReactiveViewResolver thymeleafViewResolver(ISpringWebFluxTemplateEngine templateEngine) {
public ThymeleafReactiveViewResolver thymeleafViewResolver(
ISpringWebFluxTemplateEngine templateEngine) {
ThymeleafReactiveViewResolver resolver = new ThymeleafReactiveViewResolver();
resolver.setTemplateEngine(templateEngine);
resolver.setDefaultCharset(this.properties.getEncoding());
resolver.setSupportedMediaTypes(this.properties.getReactive().getMediaTypes());
resolver.setSupportedMediaTypes(
this.properties.getReactive().getMediaTypes());
resolver.setExcludedViewNames(this.properties.getExcludedViewNames());
resolver.setViewNames(this.properties.getViewNames());
if (this.properties.getReactive().getMaxChunkSize() > 0) {
resolver.setResponseMaxChunkSizeBytes(this.properties.getReactive().getMaxChunkSize());
resolver.setResponseMaxChunkSizeBytes(
this.properties.getReactive().getMaxChunkSize());
}
// This resolver acts as a fallback resolver (e.g. like a
// InternalResourceViewResolver) so it needs to have low precedence
@ -303,7 +295,7 @@ public class ThymeleafAutoConfiguration {
}
@Configuration
@ConditionalOnClass({SpringSecurityDialect.class})
@ConditionalOnClass({ SpringSecurityDialect.class })
protected static class ThymeleafSecurityDialectConfiguration {
@Bean

@ -227,8 +227,8 @@ public class ThymeleafProperties {
/**
* Media types supported by the view technology.
*/
private List<MediaType> mediaTypes =
new ArrayList(Collections.singletonList(MediaType.TEXT_HTML));
private List<MediaType> mediaTypes = new ArrayList<>(
Collections.singletonList(MediaType.TEXT_HTML));
public List<MediaType> getMediaTypes() {
return this.mediaTypes;
@ -245,5 +245,7 @@ public class ThymeleafProperties {
public void setMaxChunkSize(int maxChunkSize) {
this.maxChunkSize = maxChunkSize;
}
}
}

@ -82,7 +82,8 @@ public class ThymeleafReactiveAutoConfigurationTests {
assertThat(resolver instanceof SpringResourceTemplateResolver).isTrue();
assertThat(((SpringResourceTemplateResolver) resolver).getCharacterEncoding())
.isEqualTo("UTF-16");
ThymeleafReactiveViewResolver views = this.context.getBean(ThymeleafReactiveViewResolver.class);
ThymeleafReactiveViewResolver views = this.context
.getBean(ThymeleafReactiveViewResolver.class);
assertThat(views.getDefaultCharset().name()).isEqualTo("UTF-16");
}
@ -90,8 +91,10 @@ public class ThymeleafReactiveAutoConfigurationTests {
public void overrideMediaTypes() throws Exception {
load(BaseConfiguration.class,
"spring.thymeleaf.reactive.media-types:text/html,text/plain");
ThymeleafReactiveViewResolver views = this.context.getBean(ThymeleafReactiveViewResolver.class);
assertThat(views.getSupportedMediaTypes()).contains(MediaType.TEXT_HTML, MediaType.TEXT_PLAIN);
ThymeleafReactiveViewResolver views = this.context
.getBean(ThymeleafReactiveViewResolver.class);
assertThat(views.getSupportedMediaTypes()).contains(MediaType.TEXT_HTML,
MediaType.TEXT_PLAIN);
}
@Test
@ -104,27 +107,31 @@ public class ThymeleafReactiveAutoConfigurationTests {
@Test
public void overrideViewNames() throws Exception {
load(BaseConfiguration.class, "spring.thymeleaf.viewNames:foo,bar");
ThymeleafReactiveViewResolver views = this.context.getBean(ThymeleafReactiveViewResolver.class);
assertThat(views.getViewNames()).isEqualTo(new String[] {"foo", "bar"});
ThymeleafReactiveViewResolver views = this.context
.getBean(ThymeleafReactiveViewResolver.class);
assertThat(views.getViewNames()).isEqualTo(new String[] { "foo", "bar" });
}
@Test
public void templateLocationDoesNotExist() throws Exception {
load(BaseConfiguration.class, "spring.thymeleaf.prefix:classpath:/no-such-directory/");
load(BaseConfiguration.class,
"spring.thymeleaf.prefix:classpath:/no-such-directory/");
this.output.expect(containsString("Cannot find template location"));
}
@Test
public void templateLocationEmpty() throws Exception {
new File("target/test-classes/templates/empty-directory").mkdir();
load(BaseConfiguration.class, "spring.thymeleaf.prefix:classpath:/templates/empty-directory/");
load(BaseConfiguration.class,
"spring.thymeleaf.prefix:classpath:/templates/empty-directory/");
this.output.expect(not(containsString("Cannot find template location")));
}
@Test
public void useDataDialect() throws Exception {
load(BaseConfiguration.class);
ISpringWebFluxTemplateEngine engine = this.context.getBean(ISpringWebFluxTemplateEngine.class);
ISpringWebFluxTemplateEngine engine = this.context
.getBean(ISpringWebFluxTemplateEngine.class);
Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar"));
String result = engine.process("data-dialect", attrs);
assertThat(result).isEqualTo("<html><body data-foo=\"bar\"></body></html>");
@ -133,7 +140,8 @@ public class ThymeleafReactiveAutoConfigurationTests {
@Test
public void useJava8TimeDialect() throws Exception {
load(BaseConfiguration.class);
ISpringWebFluxTemplateEngine engine = this.context.getBean(ISpringWebFluxTemplateEngine.class);
ISpringWebFluxTemplateEngine engine = this.context
.getBean(ISpringWebFluxTemplateEngine.class);
Context attrs = new Context(Locale.UK);
String result = engine.process("java8time-dialect", attrs);
assertThat(result).isEqualTo("<html><body>2015-11-24</body></html>");
@ -142,7 +150,8 @@ public class ThymeleafReactiveAutoConfigurationTests {
@Test
public void renderTemplate() throws Exception {
load(BaseConfiguration.class);
ISpringWebFluxTemplateEngine engine = this.context.getBean(ISpringWebFluxTemplateEngine.class);
ISpringWebFluxTemplateEngine engine = this.context
.getBean(ISpringWebFluxTemplateEngine.class);
Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar"));
String result = engine.process("home", attrs);
assertThat(result).isEqualTo("<html><body>bar</body></html>");
@ -167,7 +176,8 @@ public class ThymeleafReactiveAutoConfigurationTests {
}
@Configuration
@ImportAutoConfiguration({ThymeleafAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class})
@ImportAutoConfiguration({ ThymeleafAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class })
protected static class BaseConfiguration {
}

@ -107,12 +107,13 @@ public class ThymeleafServletAutoConfigurationTests {
public void overrideViewNames() throws Exception {
load(BaseConfiguration.class, "spring.thymeleaf.viewNames:foo,bar");
ThymeleafViewResolver views = this.context.getBean(ThymeleafViewResolver.class);
assertThat(views.getViewNames()).isEqualTo(new String[] {"foo", "bar"});
assertThat(views.getViewNames()).isEqualTo(new String[] { "foo", "bar" });
}
@Test
public void templateLocationDoesNotExist() throws Exception {
load(BaseConfiguration.class, "spring.thymeleaf.prefix:classpath:/no-such-directory/");
load(BaseConfiguration.class,
"spring.thymeleaf.prefix:classpath:/no-such-directory/");
this.output.expect(containsString("Cannot find template location"));
}
@ -230,8 +231,8 @@ public class ThymeleafServletAutoConfigurationTests {
}
@Configuration
@ImportAutoConfiguration({ThymeleafAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class})
@ImportAutoConfiguration({ ThymeleafAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class })
static class BaseConfiguration {
}

@ -2382,7 +2382,7 @@ methods. Dedicated variants exists for Tomcat, Jetty and Undertow.
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;
@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
@ -2390,7 +2390,7 @@ methods. Dedicated variants exists for Tomcat, Jetty and Undertow.
public void customize(ConfigurableServletWebServerFactory server) {
server.setPort(9000);
}
}
----

Loading…
Cancel
Save