Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
cf5d5517
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
cf5d5517
编写于
5月 16, 2012
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Pattern suffix issue in AnnotationMethodHandlerAdapter
SPR-9333
上级
ae216fbb
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
49 addition
and
13 deletion
+49
-13
spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java
...mework/web/servlet/handler/AbstractUrlHandlerMapping.java
+1
-1
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java
...ervlet/mvc/annotation/AnnotationMethodHandlerAdapter.java
+15
-8
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/DefaultAnnotationHandlerMapping.java
...rvlet/mvc/annotation/DefaultAnnotationHandlerMapping.java
+6
-2
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/annotation/UriTemplateServletAnnotationControllerTests.java
...notation/UriTemplateServletAnnotationControllerTests.java
+27
-2
未找到文件。
spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java
浏览文件 @
cf5d5517
...
...
@@ -208,7 +208,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
*/
protected
void
validateHandler
(
Object
handler
,
HttpServletRequest
request
)
throws
Exception
{
}
/**
* Build a handler object for the given raw handler, exposing the actual
* handler, the {@link #PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE}, as well as
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java
浏览文件 @
cf5d5517
...
...
@@ -588,7 +588,8 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
if
(!
typeLevelPattern
.
startsWith
(
"/"
))
{
typeLevelPattern
=
"/"
+
typeLevelPattern
;
}
if
(
getMatchingPattern
(
typeLevelPattern
,
lookupPath
)
!=
null
)
{
boolean
useSuffixPattern
=
useSuffixPattern
(
request
);
if
(
getMatchingPattern
(
typeLevelPattern
,
lookupPath
,
useSuffixPattern
)
!=
null
)
{
if
(
mappingInfo
.
matches
(
request
))
{
match
=
true
;
mappingInfo
.
addMatchedPattern
(
typeLevelPattern
);
...
...
@@ -675,6 +676,11 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
return
(
Boolean
)
request
.
getAttribute
(
HandlerMapping
.
INTROSPECT_TYPE_LEVEL_MAPPING
);
}
private
boolean
useSuffixPattern
(
HttpServletRequest
request
)
{
Object
value
=
request
.
getAttribute
(
DefaultAnnotationHandlerMapping
.
USE_DEFAULT_SUFFIX_PATTERN
);
return
(
value
!=
null
)
?
(
Boolean
)
value
:
Boolean
.
TRUE
;
}
/**
* Determines the combined pattern for the given methodLevelPattern and path.
* <p>Uses the following algorithm:
...
...
@@ -687,6 +693,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
* </ol>
*/
private
String
getCombinedPattern
(
String
methodLevelPattern
,
String
lookupPath
,
HttpServletRequest
request
)
{
boolean
useSuffixPattern
=
useSuffixPattern
(
request
);
if
(
useTypeLevelMapping
(
request
))
{
String
[]
typeLevelPatterns
=
getTypeLevelMapping
().
value
();
for
(
String
typeLevelPattern
:
typeLevelPatterns
)
{
...
...
@@ -694,7 +701,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
typeLevelPattern
=
"/"
+
typeLevelPattern
;
}
String
combinedPattern
=
pathMatcher
.
combine
(
typeLevelPattern
,
methodLevelPattern
);
String
matchingPattern
=
getMatchingPattern
(
combinedPattern
,
lookupPath
);
String
matchingPattern
=
getMatchingPattern
(
combinedPattern
,
lookupPath
,
useSuffixPattern
);
if
(
matchingPattern
!=
null
)
{
return
matchingPattern
;
}
...
...
@@ -704,20 +711,20 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
String
bestMatchingPattern
=
(
String
)
request
.
getAttribute
(
HandlerMapping
.
BEST_MATCHING_PATTERN_ATTRIBUTE
);
if
(
StringUtils
.
hasText
(
bestMatchingPattern
)
&&
bestMatchingPattern
.
endsWith
(
"*"
))
{
String
combinedPattern
=
pathMatcher
.
combine
(
bestMatchingPattern
,
methodLevelPattern
);
String
matchingPattern
=
getMatchingPattern
(
combinedPattern
,
lookupPath
);
String
matchingPattern
=
getMatchingPattern
(
combinedPattern
,
lookupPath
,
useSuffixPattern
);
if
(
matchingPattern
!=
null
&&
!
matchingPattern
.
equals
(
bestMatchingPattern
))
{
return
matchingPattern
;
}
}
return
getMatchingPattern
(
methodLevelPattern
,
lookupPath
);
return
getMatchingPattern
(
methodLevelPattern
,
lookupPath
,
useSuffixPattern
);
}
private
String
getMatchingPattern
(
String
pattern
,
String
lookupPath
)
{
private
String
getMatchingPattern
(
String
pattern
,
String
lookupPath
,
boolean
useSuffixPattern
)
{
if
(
pattern
.
equals
(
lookupPath
))
{
return
pattern
;
}
boolean
hasSuffix
=
pattern
.
indexOf
(
'.'
)
!=
-
1
;
if
(!
hasSuffix
)
{
if
(
useSuffixPattern
&&
!
hasSuffix
)
{
String
patternWithSuffix
=
pattern
+
".*"
;
if
(
pathMatcher
.
match
(
patternWithSuffix
,
lookupPath
))
{
return
patternWithSuffix
;
...
...
@@ -727,7 +734,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
return
pattern
;
}
boolean
endsWithSlash
=
pattern
.
endsWith
(
"/"
);
if
(!
endsWithSlash
)
{
if
(
useSuffixPattern
&&
!
endsWithSlash
)
{
String
patternWithSlash
=
pattern
+
"/"
;
if
(
pathMatcher
.
match
(
patternWithSlash
,
lookupPath
))
{
return
patternWithSlash
;
...
...
@@ -1236,7 +1243,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
private
int
compareAcceptHeaders
(
RequestMappingInfo
info1
,
RequestMappingInfo
info2
)
{
List
<
MediaType
>
requestAccepts
=
request
.
getHeaders
().
getAccept
();
MediaType
.
sortByQualityValue
(
requestAccepts
);
List
<
MediaType
>
info1Accepts
=
getAcceptHeaderValue
(
info1
);
List
<
MediaType
>
info2Accepts
=
getAcceptHeaderValue
(
info2
);
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/DefaultAnnotationHandlerMapping.java
浏览文件 @
cf5d5517
/*
* Copyright 2002-201
0
the original author or authors.
* Copyright 2002-201
2
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -22,6 +22,7 @@ import java.util.HashMap;
import
java.util.LinkedHashSet
;
import
java.util.Map
;
import
java.util.Set
;
import
javax.servlet.http.HttpServletRequest
;
import
org.springframework.context.ApplicationContext
;
...
...
@@ -81,9 +82,11 @@ import org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMappin
*/
public
class
DefaultAnnotationHandlerMapping
extends
AbstractDetectingUrlHandlerMapping
{
static
final
String
USE_DEFAULT_SUFFIX_PATTERN
=
DefaultAnnotationHandlerMapping
.
class
.
getName
()
+
".useDefaultSuffixPattern"
;
private
boolean
useDefaultSuffixPattern
=
true
;
private
final
Map
<
Class
,
RequestMapping
>
cachedMappings
=
new
HashMap
<
Class
,
RequestMapping
>();
private
final
Map
<
Class
<?>,
RequestMapping
>
cachedMappings
=
new
HashMap
<
Class
<?>
,
RequestMapping
>();
/**
...
...
@@ -229,6 +232,7 @@ public class DefaultAnnotationHandlerMapping extends AbstractDetectingUrlHandler
if
(
mapping
!=
null
)
{
validateMapping
(
mapping
,
request
);
}
request
.
setAttribute
(
USE_DEFAULT_SUFFIX_PATTERN
,
this
.
useDefaultSuffixPattern
);
}
/**
...
...
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/annotation/UriTemplateServletAnnotationControllerTests.java
浏览文件 @
cf5d5517
/*
* Copyright 2002-201
1
the original author or authors.
* Copyright 2002-201
2
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -351,6 +351,31 @@ public class UriTemplateServletAnnotationControllerTests {
assertEquals
(
"foo-foo"
,
response
.
getContentAsString
());
}
// SPR-9333
@Test
@SuppressWarnings
(
"serial"
)
public
void
suppressDefaultSuffixPattern
()
throws
Exception
{
servlet
=
new
DispatcherServlet
()
{
@Override
protected
WebApplicationContext
createWebApplicationContext
(
WebApplicationContext
parent
)
throws
BeansException
{
GenericWebApplicationContext
wac
=
new
GenericWebApplicationContext
();
wac
.
registerBeanDefinition
(
"controller"
,
new
RootBeanDefinition
(
VariableNamesController
.
class
));
RootBeanDefinition
mappingDef
=
new
RootBeanDefinition
(
DefaultAnnotationHandlerMapping
.
class
);
mappingDef
.
getPropertyValues
().
add
(
"useDefaultSuffixPattern"
,
false
);
wac
.
registerBeanDefinition
(
"handlerMapping"
,
mappingDef
);
wac
.
refresh
();
return
wac
;
}
};
servlet
.
init
(
new
MockServletConfig
());
MockHttpServletRequest
request
=
new
MockHttpServletRequest
(
"GET"
,
"/test/jsmith@mail.com"
);
MockHttpServletResponse
response
=
new
MockHttpServletResponse
();
servlet
.
service
(
request
,
response
);
assertEquals
(
"foo-jsmith@mail.com"
,
response
.
getContentAsString
());
}
// SPR-6906
@Test
public
void
controllerClassName
()
throws
Exception
{
...
...
@@ -389,7 +414,7 @@ public class UriTemplateServletAnnotationControllerTests {
@Test
public
void
doIt
()
throws
Exception
{
initServlet
(
Spr6978Controller
.
class
);
MockHttpServletRequest
request
=
new
MockHttpServletRequest
(
"GET"
,
"/foo/100"
);
MockHttpServletResponse
response
=
new
MockHttpServletResponse
();
servlet
.
service
(
request
,
response
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录