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

This commit is contained in:
Juergen Hoeller 2010-02-17 23:26:06 +00:00
parent b54a099f49
commit e74b33242b
3 changed files with 64 additions and 8 deletions

View File

@ -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;

View File

@ -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);
} }

View File

@ -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 {