Browse Source

fixed regression: method-level patterns without type-level pattern do not need to start with a slash (SPR-6598)

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3000 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/head
Juergen Hoeller 16 years ago
parent
commit
0b81a067d4
  1. 7
      org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java
  2. 11
      org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/DefaultAnnotationHandlerMapping.java
  3. 54
      org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java

7
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java

@ -487,8 +487,11 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
boolean match = false; boolean match = false;
if (mappingInfo.paths.length > 0) { if (mappingInfo.paths.length > 0) {
List<String> matchedPaths = new ArrayList<String>(mappingInfo.paths.length); List<String> matchedPaths = new ArrayList<String>(mappingInfo.paths.length);
for (String methodLevelPattern : mappingInfo.paths) { for (String mappedPattern : mappingInfo.paths) {
String matchedPattern = getMatchedPattern(methodLevelPattern, lookupPath, request); if (!hasTypeLevelMapping() && !mappedPattern.startsWith("/")) {
mappedPattern = "/" + mappedPattern;
}
String matchedPattern = getMatchedPattern(mappedPattern, lookupPath, request);
if (matchedPattern != null) { if (matchedPattern != null) {
if (mappingInfo.matches(request)) { if (mappingInfo.matches(request)) {
match = true; match = true;

11
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/DefaultAnnotationHandlerMapping.java

@ -154,11 +154,11 @@ public class DefaultAnnotationHandlerMapping extends AbstractDetectingUrlHandler
/** /**
* Derive URL mappings from the handler's method-level mappings. * Derive URL mappings from the handler's method-level mappings.
* @param handlerType the handler type to introspect * @param handlerType the handler type to introspect
* @param indicateEmpty whether the returned array should contain * @param hasTypeLevelMapping whether the method-level mappings are nested
* <code>null</code> in case of an empty {@link RequestMapping} value. * within a type-level mapping
* @return the array of mapped URLs * @return the array of mapped URLs
*/ */
protected String[] determineUrlsForHandlerMethods(Class<?> handlerType, final boolean indicateEmpty) { protected String[] determineUrlsForHandlerMethods(Class<?> handlerType, final boolean hasTypeLevelMapping) {
String[] subclassResult = determineUrlsForHandlerMethods(handlerType); String[] subclassResult = determineUrlsForHandlerMethods(handlerType);
if (subclassResult != null) { if (subclassResult != null) {
return subclassResult; return subclassResult;
@ -175,10 +175,13 @@ public class DefaultAnnotationHandlerMapping extends AbstractDetectingUrlHandler
String[] mappedPatterns = mapping.value(); String[] mappedPatterns = mapping.value();
if (mappedPatterns.length > 0) { if (mappedPatterns.length > 0) {
for (String mappedPattern : mappedPatterns) { for (String mappedPattern : mappedPatterns) {
if (!hasTypeLevelMapping && !mappedPattern.startsWith("/")) {
mappedPattern = "/" + mappedPattern;
}
addUrlsForPath(urls, mappedPattern); addUrlsForPath(urls, mappedPattern);
} }
} }
else if (indicateEmpty) { else if (hasTypeLevelMapping) {
// empty method-level RequestMapping // empty method-level RequestMapping
urls.add(null); urls.add(null);
} }

54
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java

@ -964,6 +964,32 @@ public class ServletAnnotationControllerTests {
assertEquals("mySurpriseView", response.getContentAsString()); assertEquals("mySurpriseView", response.getContentAsString());
} }
@Test
public void relativeMethodPathDispatchingController() throws Exception {
initServlet(MyRelativeMethodPathDispatchingController.class);
servlet.init(new MockServletConfig());
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myApp/myHandle");
MockHttpServletResponse response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("myView", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/yourApp/myOther");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("myOtherView", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/hisApp/myLang");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("myLangView", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/herApp/surprise.do");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("mySurpriseView", response.getContentAsString());
}
@Test @Test
public void nullCommandController() throws Exception { public void nullCommandController() throws Exception {
initServlet(MyNullCommandController.class); initServlet(MyNullCommandController.class);
@ -1360,8 +1386,8 @@ public class ServletAnnotationControllerTests {
} }
/* /*
* Controllers * Controllers
*/ */
@RequestMapping("/myPath.do") @RequestMapping("/myPath.do")
private static class MyController extends AbstractController { private static class MyController extends AbstractController {
@ -1836,6 +1862,30 @@ public class ServletAnnotationControllerTests {
} }
} }
@Controller
private static class MyRelativeMethodPathDispatchingController {
@RequestMapping("**/myHandle")
public void myHandle(HttpServletResponse response) throws IOException {
response.getWriter().write("myView");
}
@RequestMapping("/**/*Other")
public void myOtherHandle(HttpServletResponse response) throws IOException {
response.getWriter().write("myOtherView");
}
@RequestMapping("**/myLang")
public void myLangHandle(HttpServletResponse response) throws IOException {
response.getWriter().write("myLangView");
}
@RequestMapping("/**/surprise")
public void mySurpriseHandle(HttpServletResponse response) throws IOException {
response.getWriter().write("mySurpriseView");
}
}
@Controller @Controller
private static class MyNullCommandController { private static class MyNullCommandController {

Loading…
Cancel
Save