Commit 066533de authored by Eddú Meléndez's avatar Eddú Meléndez Committed by Phillip Webb

Add `spring.mvc.static-path-pattern` property

Add a `spring.mvc.static-path-pattern` property which can be used to
configure the path pattern used to serve static resources.

Fixes gh-4444
Closes gh-4448
parent 854dadc6
...@@ -98,6 +98,7 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver; ...@@ -98,6 +98,7 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver;
* @author Dave Syer * @author Dave Syer
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Sébastien Deleuze * @author Sébastien Deleuze
* @author Eddú Meléndez
*/ */
@Configuration @Configuration
@ConditionalOnWebApplication @ConditionalOnWebApplication
...@@ -256,8 +257,9 @@ public class WebMvcAutoConfiguration { ...@@ -256,8 +257,9 @@ public class WebMvcAutoConfiguration {
.addResourceLocations("classpath:/META-INF/resources/webjars/") .addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(cachePeriod)); .setCachePeriod(cachePeriod));
} }
if (!registry.hasMappingForPattern("/**")) { String staticPathPattern = this.mvcProperties.getStaticPathPattern();
registerResourceChain(registry.addResourceHandler("/**") if (!registry.hasMappingForPattern(staticPathPattern)) {
registerResourceChain(registry.addResourceHandler(staticPathPattern)
.addResourceLocations( .addResourceLocations(
this.resourceProperties.getStaticLocations()) this.resourceProperties.getStaticLocations())
.setCachePeriod(cachePeriod)); .setCachePeriod(cachePeriod));
......
...@@ -31,6 +31,7 @@ import org.springframework.validation.DefaultMessageCodesResolver; ...@@ -31,6 +31,7 @@ import org.springframework.validation.DefaultMessageCodesResolver;
* @author Phillip Webb * @author Phillip Webb
* @author Sébastien Deleuze * @author Sébastien Deleuze
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Eddú Meléndez
* @since 1.1 * @since 1.1
*/ */
@ConfigurationProperties("spring.mvc") @ConfigurationProperties("spring.mvc")
...@@ -77,6 +78,11 @@ public class WebMvcProperties { ...@@ -77,6 +78,11 @@ public class WebMvcProperties {
*/ */
private Map<String, MediaType> mediaTypes = new LinkedHashMap<String, MediaType>(); private Map<String, MediaType> mediaTypes = new LinkedHashMap<String, MediaType>();
/**
* Path that pattern used for static resources.
*/
private String staticPathPattern = "/**";
private final Async async = new Async(); private final Async async = new Async();
private final View view = new View(); private final View view = new View();
...@@ -147,6 +153,14 @@ public class WebMvcProperties { ...@@ -147,6 +153,14 @@ public class WebMvcProperties {
this.dispatchTraceRequest = dispatchTraceRequest; this.dispatchTraceRequest = dispatchTraceRequest;
} }
public String getStaticPathPattern() {
return this.staticPathPattern;
}
public void setStaticPathPattern(String staticPathPattern) {
this.staticPathPattern = staticPathPattern;
}
public Async getAsync() { public Async getAsync() {
return this.async; return this.async;
} }
......
...@@ -101,6 +101,7 @@ import static org.junit.Assert.assertTrue; ...@@ -101,6 +101,7 @@ import static org.junit.Assert.assertTrue;
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Brian Clozel * @author Brian Clozel
* @author Eddú Meléndez
*/ */
public class WebMvcAutoConfigurationTests { public class WebMvcAutoConfigurationTests {
...@@ -149,6 +150,14 @@ public class WebMvcAutoConfigurationTests { ...@@ -149,6 +150,14 @@ public class WebMvcAutoConfigurationTests {
assertThat(getResourceTransformers("/**").size(), equalTo(0)); assertThat(getResourceTransformers("/**").size(), equalTo(0));
} }
@Test
public void customResourceHandlerMapping() throws Exception {
load("spring.mvc.static-path-pattern:/static/**");
Map<String, List<Resource>> mappingLocations = getResourceMappingLocations();
assertThat(mappingLocations.get("/static/**").size(), equalTo(5));
assertThat(getResourceResolvers("/static/**").size(), equalTo(1));
}
@Test @Test
public void resourceHandlerMappingOverrideWebjars() throws Exception { public void resourceHandlerMappingOverrideWebjars() throws Exception {
load(WebJars.class); load(WebJars.class);
......
...@@ -310,6 +310,7 @@ content into your application; rather pick only the properties that you need. ...@@ -310,6 +310,7 @@ content into your application; rather pick only the properties that you need.
spring.mvc.locale= # Locale to use. spring.mvc.locale= # Locale to use.
spring.mvc.media-types.*= # Maps file extensions to media types for content negotiation. spring.mvc.media-types.*= # Maps file extensions to media types for content negotiation.
spring.mvc.message-codes-resolver-format= # Formatting strategy for message codes. For instance `PREFIX_ERROR_CODE`. spring.mvc.message-codes-resolver-format= # Formatting strategy for message codes. For instance `PREFIX_ERROR_CODE`.
spring.mvc.static-path-pattern=/** # Path that pattern used for static resources.
spring.mvc.throw-exception-if-no-handler-found=false # If a "NoHandlerFoundException" should be thrown if no Handler was found to process a request. spring.mvc.throw-exception-if-no-handler-found=false # If a "NoHandlerFoundException" should be thrown if no Handler was found to process a request.
spring.mvc.view.prefix= # Spring MVC view prefix. spring.mvc.view.prefix= # Spring MVC view prefix.
spring.mvc.view.suffix= # Spring MVC view suffix. spring.mvc.view.suffix= # Spring MVC view suffix.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment