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
This commit is contained in:
Eddú Meléndez 2015-11-12 11:11:04 -05:00 committed by Phillip Webb
parent 854dadc6af
commit 066533de7e
4 changed files with 28 additions and 2 deletions

View File

@ -98,6 +98,7 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver;
* @author Dave Syer
* @author Andy Wilkinson
* @author Sébastien Deleuze
* @author Eddú Meléndez
*/
@Configuration
@ConditionalOnWebApplication
@ -256,8 +257,9 @@ public class WebMvcAutoConfiguration {
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(cachePeriod));
}
if (!registry.hasMappingForPattern("/**")) {
registerResourceChain(registry.addResourceHandler("/**")
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
registerResourceChain(registry.addResourceHandler(staticPathPattern)
.addResourceLocations(
this.resourceProperties.getStaticLocations())
.setCachePeriod(cachePeriod));

View File

@ -31,6 +31,7 @@ import org.springframework.validation.DefaultMessageCodesResolver;
* @author Phillip Webb
* @author Sébastien Deleuze
* @author Stephane Nicoll
* @author Eddú Meléndez
* @since 1.1
*/
@ConfigurationProperties("spring.mvc")
@ -77,6 +78,11 @@ public class WebMvcProperties {
*/
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 View view = new View();
@ -147,6 +153,14 @@ public class WebMvcProperties {
this.dispatchTraceRequest = dispatchTraceRequest;
}
public String getStaticPathPattern() {
return this.staticPathPattern;
}
public void setStaticPathPattern(String staticPathPattern) {
this.staticPathPattern = staticPathPattern;
}
public Async getAsync() {
return this.async;
}

View File

@ -101,6 +101,7 @@ import static org.junit.Assert.assertTrue;
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Brian Clozel
* @author Eddú Meléndez
*/
public class WebMvcAutoConfigurationTests {
@ -149,6 +150,14 @@ public class WebMvcAutoConfigurationTests {
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
public void resourceHandlerMappingOverrideWebjars() throws Exception {
load(WebJars.class);

View File

@ -310,6 +310,7 @@ content into your application; rather pick only the properties that you need.
spring.mvc.locale= # Locale to use.
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.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.view.prefix= # Spring MVC view prefix.
spring.mvc.view.suffix= # Spring MVC view suffix.