SPR-5251: URI Templates support relative @RequestMappings (on class level, with more specific mapping on method level)

This commit is contained in:
Arjen Poutsma
2008-11-19 17:08:34 +00:00
parent 2b2805058b
commit 27ed13f44d
2 changed files with 73 additions and 4 deletions

View File

@@ -51,6 +51,7 @@ import org.springframework.ui.Model;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.PathMatcher;
import org.springframework.util.StringUtils;
import org.springframework.validation.support.BindingAwareModelMap;
@@ -503,6 +504,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
}
}
if (targetHandlerMethods.size() == 1) {
extractHandlerMethodUriTemplates(targetPathMatches.values().iterator().next(), lookupPath, request);
return targetHandlerMethods.values().iterator().next();
}
else if (!targetHandlerMethods.isEmpty()) {
@@ -525,6 +527,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
}
}
}
extractHandlerMethodUriTemplates(bestPathMatch, lookupPath, request);
return targetHandlerMethods.get(bestMappingMatch);
}
else {
@@ -570,6 +573,37 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
private boolean isBetterParamMatch(RequestMappingInfo mapping, RequestMappingInfo mappingToCompare) {
return (mappingToCompare.params.length < mapping.params.length);
}
@SuppressWarnings("unchecked")
private void extractHandlerMethodUriTemplates(String mappedPath, String lookupPath, HttpServletRequest request) {
Map<String, String> variables = null;
boolean hasSuffix = (mappedPath.indexOf('.') != -1);
if (!hasSuffix && pathMatcher.match(mappedPath + ".*", lookupPath)) {
String realPath = mappedPath + ".*";
if (pathMatcher.match(realPath, lookupPath)) {
variables = pathMatcher.extractUriTemplateVariables(realPath, lookupPath);
}
}
if (variables == null && !mappedPath.startsWith("/")) {
String realPath = "/**/" + mappedPath;
if (pathMatcher.match(realPath, lookupPath)) {
variables = pathMatcher.extractUriTemplateVariables(realPath, lookupPath);
} else {
realPath = realPath + ".*";
if (pathMatcher.match(realPath, lookupPath)) {
variables = pathMatcher.extractUriTemplateVariables(realPath, lookupPath);
}
}
}
if (!CollectionUtils.isEmpty(variables)) {
Map<String, String> typeVariables =
(Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
if (typeVariables != null) {
variables.putAll(typeVariables);
}
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, variables);
}
}
}