diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java index 9f77d3d2c3e..fd1a3845773 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java @@ -25,6 +25,7 @@ import javax.servlet.http.HttpServletRequest; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; +import org.springframework.web.context.support.ServletContextResource; /** * A simple {@code ResourceResolver} that tries to find a resource under the given @@ -172,6 +173,10 @@ public class PathResourceResolver extends AbstractResourceResolver { resourcePath = resource.getURL().toExternalForm(); locationPath = location.getURL().toExternalForm(); } + else if(resource instanceof ServletContextResource) { + resourcePath = ((ServletContextResource) resource).getPath(); + locationPath = ((ServletContextResource) location).getPath(); + } else { resourcePath = resource.getURL().getPath(); locationPath = location.getURL().getPath(); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/PathResourceResolverTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/PathResourceResolverTests.java index 26218e33779..e93ec912039 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/PathResourceResolverTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/PathResourceResolverTests.java @@ -15,10 +15,7 @@ */ package org.springframework.web.servlet.resource; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; import java.io.IOException; import java.util.Arrays; @@ -28,6 +25,8 @@ import org.junit.Test; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; +import org.springframework.mock.web.test.MockServletContext; +import org.springframework.web.context.support.ServletContextResource; /** * Unit tests for @@ -93,6 +92,19 @@ public class PathResourceResolverTests { assertEquals("../testalternatepath/bar.css", actual); } + // SPR-12432 + @Test + public void checkServletContextResource() throws Exception { + Resource classpathLocation = new ClassPathResource("test/", PathResourceResolver.class); + MockServletContext context = new MockServletContext(); + + ServletContextResource servletContextLocation = new ServletContextResource(context, "/webjars/"); + ServletContextResource resource = new ServletContextResource(context, "/webjars/webjar-foo/1.0/foo.js"); + + assertFalse(this.resolver.checkResource(resource, classpathLocation)); + assertTrue(this.resolver.checkResource(resource, servletContextLocation)); + } + private void testCheckResource(Resource location, String requestPath) throws IOException { Resource actual = this.resolver.resolveResource(null, requestPath, Arrays.asList(location), null); assertTrue(location.createRelative(requestPath).exists());