Merge pull request #5162 from Kazuki Shimizu

* gh-5126:
  Auto-configure ResourceUrlEncodingFilter when using FreeMarker
pull/5134/head
Andy Wilkinson 9 years ago
commit a754e63e46

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.template.TemplateLocation; 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.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@ -42,6 +43,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory; import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean; 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.FreeMarkerConfig;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
@ -51,6 +53,7 @@ import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
* *
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Dave Syer * @author Dave Syer
* @author Kazuki Shimizu
* @since 1.1.0 * @since 1.1.0
*/ */
@Configuration @Configuration
@ -149,5 +152,12 @@ public class FreeMarkerAutoConfiguration {
return resolver; return resolver;
} }
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledResourceChain
public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
return new ResourceUrlEncodingFilter();
}
} }
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.test.util.ReflectionTestUtils;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.View; 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.support.RequestContext;
import org.springframework.web.servlet.view.AbstractTemplateViewResolver; import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
@ -48,6 +49,7 @@ import static org.hamcrest.Matchers.containsString;
* Tests for {@link FreeMarkerAutoConfiguration}. * Tests for {@link FreeMarkerAutoConfiguration}.
* *
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Kazuki Shimizu
*/ */
public class FreeMarkerAutoConfigurationTests { 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) { private void registerAndRefreshContext(String... env) {
EnvironmentTestUtils.addEnvironment(this.context, env); EnvironmentTestUtils.addEnvironment(this.context, env);
this.context.register(FreeMarkerAutoConfiguration.class); this.context.register(FreeMarkerAutoConfiguration.class);

@ -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 NOTE: Links to resources are rewritten at runtime in template, thanks to a
`ResourceUrlEncodingFilter`, auto-configured for Thymeleaf and Velocity. You should `ResourceUrlEncodingFilter`, auto-configured for Thymeleaf, Velocity and FreeMarker. You
manually declare this filter when using JSPs. Other template engines aren't automatically should manually declare this filter when using JSPs. Other template engines aren't
supported right now, but can be with custom template macros/helpers and the use of the 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`]. {spring-javadoc}/web/servlet/resource/ResourceUrlProvider.{dc-ext}[`ResourceUrlProvider`].
When loading resources dynamically with, for example, a JavaScript module loader, renaming When loading resources dynamically with, for example, a JavaScript module loader, renaming

Loading…
Cancel
Save