Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
dk131072
spring-framework
提交
aa46d181
S
spring-framework
项目概览
dk131072
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
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,发现更多精彩内容 >>
提交
aa46d181
编写于
8月 15, 2011
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SPR-6464 FlashMap polish
上级
224bce1b
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
40 addition
and
35 deletion
+40
-35
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/FlashMap.java
...c/main/java/org/springframework/web/servlet/FlashMap.java
+21
-17
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/RedirectView.java
...va/org/springframework/web/servlet/view/RedirectView.java
+1
-1
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/FlashMapTests.java
...t/java/org/springframework/web/servlet/FlashMapTests.java
+16
-15
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/support/DefaultFlashMapManagerTests.java
...work/web/servlet/support/DefaultFlashMapManagerTests.java
+2
-2
未找到文件。
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/FlashMap.java
浏览文件 @
aa46d181
...
...
@@ -37,7 +37,7 @@ public class FlashMap extends HashMap<String, Object> implements Comparable<Flas
private
static
final
long
serialVersionUID
=
1L
;
private
String
expected
UrlPath
;
private
String
expected
RequestUri
;
private
final
Map
<
String
,
String
>
expectedRequestParameters
=
new
LinkedHashMap
<
String
,
String
>();
...
...
@@ -57,24 +57,27 @@ public class FlashMap extends HashMap<String, Object> implements Comparable<Flas
* @param request the current request, used to normalize relative URLs
* @param url an absolute URL, a URL path, or a relative URL, never {@code null}
*/
public
FlashMap
setExpected
Url
(
HttpServletRequest
request
,
String
url
)
{
public
FlashMap
setExpected
RequestUri
(
HttpServletRequest
request
,
String
url
)
{
Assert
.
notNull
(
url
,
"Expected URL must not be null"
);
String
urlPath
=
extractUrlPath
(
url
);
this
.
expected
UrlPath
=
urlPath
.
startsWith
(
"/"
)
?
urlPath
:
normalizeRelativeUrl
(
request
,
urlP
ath
);
String
path
=
extractRequestUri
(
url
);
this
.
expected
RequestUri
=
path
.
startsWith
(
"/"
)
?
path
:
normalizeRelativePath
(
request
,
p
ath
);
return
this
;
}
private
String
extract
UrlPath
(
String
url
)
{
int
index
=
url
.
indexOf
(
"
://
"
);
private
String
extract
RequestUri
(
String
url
)
{
int
index
=
url
.
indexOf
(
"
?
"
);
if
(
index
!=
-
1
)
{
index
=
url
.
indexOf
(
'/'
,
index
+
3
);
url
=
(
index
!=
-
1
)
?
url
.
substring
(
index
)
:
""
;
url
=
url
.
substring
(
0
,
index
);
}
index
=
url
.
indexOf
(
'?'
);
return
(
index
!=
-
1
)
?
url
.
substring
(
0
,
index
)
:
url
;
index
=
url
.
indexOf
(
"://"
);
if
(
index
!=
-
1
)
{
int
pathBegin
=
url
.
indexOf
(
"/"
,
index
+
3
);
url
=
(
pathBegin
!=
-
1
)
?
url
.
substring
(
pathBegin
)
:
""
;
}
return
url
;
}
private
String
normalizeRelative
Url
(
HttpServletRequest
request
,
String
relativeUrl
)
{
private
String
normalizeRelative
Path
(
HttpServletRequest
request
,
String
relativeUrl
)
{
String
requestUri
=
this
.
urlPathHelper
.
getRequestUri
(
request
);
relativeUrl
=
requestUri
.
substring
(
0
,
requestUri
.
lastIndexOf
(
'/'
)
+
1
)
+
relativeUrl
;
return
StringUtils
.
cleanPath
(
relativeUrl
);
...
...
@@ -117,7 +120,7 @@ public class FlashMap extends HashMap<String, Object> implements Comparable<Flas
/**
* Whether this FlashMap matches to the given request by checking
* expectations provided via {@link #setExpected
Url
} and
* expectations provided via {@link #setExpected
RequestUri
} and
* {@link #setExpectedRequestParams}.
*
* @param request the current request
...
...
@@ -125,8 +128,9 @@ public class FlashMap extends HashMap<String, Object> implements Comparable<Flas
* @return "true" if the expectations match or there are no expectations.
*/
public
boolean
matches
(
HttpServletRequest
request
)
{
if
(
this
.
expectedUrlPath
!=
null
)
{
if
(!
matchPathsIgnoreTrailingSlash
(
this
.
urlPathHelper
.
getRequestUri
(
request
),
this
.
expectedUrlPath
))
{
if
(
this
.
expectedRequestUri
!=
null
)
{
String
requestUri
=
this
.
urlPathHelper
.
getRequestUri
(
request
);
if
(!
matchPathsIgnoreTrailingSlash
(
requestUri
,
this
.
expectedRequestUri
))
{
return
false
;
}
}
...
...
@@ -178,8 +182,8 @@ public class FlashMap extends HashMap<String, Object> implements Comparable<Flas
* current request via {@link FlashMap#matches}.
*/
public
int
compareTo
(
FlashMap
other
)
{
int
thisUrlPath
=
(
this
.
expected
UrlPath
!=
null
)
?
1
:
0
;
int
otherUrlPath
=
(
other
.
expected
UrlPath
!=
null
)
?
1
:
0
;
int
thisUrlPath
=
(
this
.
expected
RequestUri
!=
null
)
?
1
:
0
;
int
otherUrlPath
=
(
other
.
expected
RequestUri
!=
null
)
?
1
:
0
;
if
(
thisUrlPath
!=
otherUrlPath
)
{
return
otherUrlPath
-
thisUrlPath
;
}
...
...
@@ -192,7 +196,7 @@ public class FlashMap extends HashMap<String, Object> implements Comparable<Flas
public
String
toString
()
{
StringBuilder
result
=
new
StringBuilder
();
result
.
append
(
"[Attributes="
).
append
(
super
.
toString
());
result
.
append
(
", expecte
UrlPath="
).
append
(
this
.
expectedUrlPath
);
result
.
append
(
", expecte
RequestUri="
).
append
(
this
.
expectedRequestUri
);
result
.
append
(
", expectedRequestParameters="
+
this
.
expectedRequestParameters
.
toString
()).
append
(
"]"
);
return
result
.
toString
();
}
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/RedirectView.java
浏览文件 @
aa46d181
...
...
@@ -264,7 +264,7 @@ public class RedirectView extends AbstractUrlBasedView {
FlashMap
flashMap
=
RequestContextUtils
.
getFlashMap
(
request
);
if
(!
CollectionUtils
.
isEmpty
(
flashMap
))
{
flashMap
.
setExpected
UrlPath
(
request
,
targetUrl
.
toString
());
flashMap
.
setExpected
RequestUri
(
request
,
targetUrl
.
toString
());
}
if
(
this
.
exposeModelAttributes
)
{
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/FlashMapTests.java
浏览文件 @
aa46d181
...
...
@@ -22,8 +22,6 @@ import static org.junit.Assert.assertTrue;
import
org.junit.Test
;
import
org.springframework.mock.web.MockHttpServletRequest
;
import
org.springframework.ui.ModelMap
;
import
org.springframework.web.servlet.FlashMap
;
/**
* Test fixture for {@link FlashMap} tests.
...
...
@@ -44,14 +42,14 @@ public class FlashMapTests {
@Test
public
void
matchUrlPath
()
{
FlashMap
flashMap
=
new
FlashMap
();
flashMap
.
setExpected
Url
(
null
,
"/yes"
);
flashMap
.
setExpected
RequestUri
(
null
,
"/yes"
);
assertTrue
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/yes"
)));
assertTrue
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/yes/"
)));
assertFalse
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/yes/but"
)));
assertFalse
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/no"
)));
flashMap
.
setExpected
Url
(
null
,
"/thats it?"
);
flashMap
.
setExpected
RequestUri
(
null
,
"/thats it?"
);
assertTrue
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/thats it"
)));
assertTrue
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/thats%20it"
)));
...
...
@@ -61,45 +59,48 @@ public class FlashMapTests {
public
void
matchRelativeUrlPath
()
{
FlashMap
flashMap
=
new
FlashMap
();
flashMap
.
setExpected
Url
(
new
MockHttpServletRequest
(
"GET"
,
"/oh/no"
),
"yes"
);
flashMap
.
setExpected
RequestUri
(
new
MockHttpServletRequest
(
"GET"
,
"/oh/no"
),
"yes"
);
assertTrue
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/oh/yes"
)));
flashMap
.
setExpected
Url
(
new
MockHttpServletRequest
(
"GET"
,
"/oh/not/again"
),
"../ok"
);
flashMap
.
setExpected
RequestUri
(
new
MockHttpServletRequest
(
"GET"
,
"/oh/not/again"
),
"../ok"
);
assertTrue
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/oh/ok"
)));
flashMap
.
setExpected
Url
(
new
MockHttpServletRequest
(
"GET"
,
"/yes/it/is"
),
".."
);
flashMap
.
setExpected
RequestUri
(
new
MockHttpServletRequest
(
"GET"
,
"/yes/it/is"
),
".."
);
assertTrue
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/yes"
)));
flashMap
.
setExpected
Url
(
new
MockHttpServletRequest
(
"GET"
,
"/yes/it/is"
),
"../"
);
flashMap
.
setExpected
RequestUri
(
new
MockHttpServletRequest
(
"GET"
,
"/yes/it/is"
),
"../"
);
assertTrue
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/yes"
)));
flashMap
.
setExpected
Url
(
new
MockHttpServletRequest
(
"GET"
,
"/thats it/really"
),
"./"
);
flashMap
.
setExpected
RequestUri
(
new
MockHttpServletRequest
(
"GET"
,
"/thats it/really"
),
"./"
);
assertTrue
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/thats%20it"
)));
flashMap
.
setExpectedRequestUri
(
new
MockHttpServletRequest
(
"GET"
,
"/yes/it/is"
),
"..?url=http://example.com"
);
assertTrue
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/yes"
)));
}
@Test
public
void
matchAbsoluteUrlPath
()
{
FlashMap
flashMap
=
new
FlashMap
();
flashMap
.
setExpected
Url
(
new
MockHttpServletRequest
(),
"http://example.com"
);
flashMap
.
setExpected
RequestUri
(
new
MockHttpServletRequest
(),
"http://example.com"
);
assertTrue
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
""
)));
assertTrue
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/"
)));
assertFalse
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/no"
)));
flashMap
.
setExpected
Url
(
null
,
"http://example.com/"
);
flashMap
.
setExpected
RequestUri
(
null
,
"http://example.com/"
);
assertTrue
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
""
)));
assertTrue
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/"
)));
assertFalse
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/no"
)));
flashMap
.
setExpected
Url
(
null
,
"http://example.com/yes"
);
flashMap
.
setExpected
RequestUri
(
null
,
"http://example.com/yes"
);
assertTrue
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/yes"
)));
assertTrue
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/yes/"
)));
assertFalse
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/no"
)));
assertFalse
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/yes/no"
)));
flashMap
.
setExpected
Url
(
null
,
"http://example.com/yes?a=1"
);
flashMap
.
setExpected
RequestUri
(
null
,
"http://example.com/yes?a=1"
);
assertTrue
(
flashMap
.
matches
(
new
MockHttpServletRequest
(
"GET"
,
"/yes"
)));
}
...
...
@@ -151,11 +152,11 @@ public class FlashMapTests {
FlashMap
flashMap2
=
new
FlashMap
();
assertEquals
(
0
,
flashMap1
.
compareTo
(
flashMap2
));
flashMap1
.
setExpected
Url
(
null
,
"/path1"
);
flashMap1
.
setExpected
RequestUri
(
null
,
"/path1"
);
assertEquals
(-
1
,
flashMap1
.
compareTo
(
flashMap2
));
assertEquals
(
1
,
flashMap2
.
compareTo
(
flashMap1
));
flashMap2
.
setExpected
Url
(
null
,
"/path2"
);
flashMap2
.
setExpected
RequestUri
(
null
,
"/path2"
);
assertEquals
(
0
,
flashMap1
.
compareTo
(
flashMap2
));
flashMap1
.
setExpectedRequestParam
(
"id"
,
"1"
);
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/support/DefaultFlashMapManagerTests.java
浏览文件 @
aa46d181
...
...
@@ -79,10 +79,10 @@ public class DefaultFlashMapManagerTests {
FlashMap
emptyFlashMap
=
new
FlashMap
();
FlashMap
oneFlashMap
=
new
FlashMap
();
oneFlashMap
.
setExpected
Url
(
null
,
"/one"
);
oneFlashMap
.
setExpected
RequestUri
(
null
,
"/one"
);
FlashMap
oneOtherFlashMap
=
new
FlashMap
();
oneOtherFlashMap
.
setExpected
Url
(
null
,
"/one/other"
);
oneOtherFlashMap
.
setExpected
RequestUri
(
null
,
"/one/other"
);
List
<
FlashMap
>
allMaps
=
createFlashMapsSessionAttribute
();
allMaps
.
add
(
emptyFlashMap
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录