diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfiguration.java index 5177abaa7d..825a683584 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 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. @@ -35,6 +35,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebAppli import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.template.TemplateLocation; +import org.springframework.boot.autoconfigure.web.ConditionalOnEnabledResourceChain; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.ApplicationContext; @@ -42,6 +43,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory; import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean; +import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; @@ -51,6 +53,7 @@ import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; * * @author Andy Wilkinson * @author Dave Syer + * @author Kazuki Shimizu * @since 1.1.0 */ @Configuration @@ -149,5 +152,12 @@ public class FreeMarkerAutoConfiguration { return resolver; } + @Bean + @ConditionalOnMissingBean + @ConditionalOnEnabledResourceChain + public ResourceUrlEncodingFilter resourceUrlEncodingFilter() { + return new ResourceUrlEncodingFilter(); + } + } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationTests.java index efda973961..8dccbf17f4 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2016 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. @@ -36,6 +36,7 @@ import org.springframework.mock.web.MockServletContext; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.View; +import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter; import org.springframework.web.servlet.support.RequestContext; import org.springframework.web.servlet.view.AbstractTemplateViewResolver; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; @@ -48,6 +49,7 @@ import static org.hamcrest.Matchers.containsString; * Tests for {@link FreeMarkerAutoConfiguration}. * * @author Andy Wilkinson + * @author Kazuki Shimizu */ public class FreeMarkerAutoConfigurationTests { @@ -189,6 +191,20 @@ public class FreeMarkerAutoConfigurationTests { } } + @Test + public void registerResourceHandlingFilterDisabledByDefault() throws Exception { + registerAndRefreshContext(); + assertThat(this.context.getBeansOfType(ResourceUrlEncodingFilter.class)) + .isEmpty(); + } + + @Test + public void registerResourceHandlingFilterOnlyIfResourceChainIsEnabled() + throws Exception { + registerAndRefreshContext("spring.resources.chain.enabled:true"); + assertThat(this.context.getBean(ResourceUrlEncodingFilter.class)).isNotNull(); + } + private void registerAndRefreshContext(String... env) { EnvironmentTestUtils.addEnvironment(this.context, env); this.context.register(FreeMarkerAutoConfiguration.class); diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index fd0080cd9e..c31be06552 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -1509,9 +1509,10 @@ for all static resources, effectively adding a content hash in URLs, such as ---- NOTE: Links to resources are rewritten at runtime in template, thanks to a -`ResourceUrlEncodingFilter`, auto-configured for Thymeleaf and Velocity. You should -manually declare this filter when using JSPs. Other template engines aren't automatically -supported right now, but can be with custom template macros/helpers and the use of the +`ResourceUrlEncodingFilter`, auto-configured for Thymeleaf, Velocity and FreeMarker. You +should manually declare this filter when using JSPs. Other template engines aren't +automatically supported right now, but can be with custom template macros/helpers and the +use of the {spring-javadoc}/web/servlet/resource/ResourceUrlProvider.{dc-ext}[`ResourceUrlProvider`]. When loading resources dynamically with, for example, a JavaScript module loader, renaming