Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
103ac035
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,发现更多精彩内容 >>
提交
103ac035
编写于
2月 18, 2010
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
DefaultRequestToViewNameTranslator strips trailing slashes as well (SPR-6830)
上级
fff4c77d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
52 addition
and
25 deletion
+52
-25
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/DefaultRequestToViewNameTranslator.java
.../web/servlet/view/DefaultRequestToViewNameTranslator.java
+14
-7
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/DefaultRequestToViewNameTranslatorTests.java
...servlet/view/DefaultRequestToViewNameTranslatorTests.java
+38
-18
未找到文件。
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/DefaultRequestToViewNameTranslator.java
浏览文件 @
103ac035
/*
* Copyright 2002-20
07
the original author or authors.
* Copyright 2002-20
10
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.
...
...
@@ -66,6 +66,8 @@ public class DefaultRequestToViewNameTranslator implements RequestToViewNameTran
private
boolean
stripLeadingSlash
=
true
;
private
boolean
stripTrailingSlash
=
true
;
private
boolean
stripExtension
=
true
;
private
UrlPathHelper
urlPathHelper
=
new
UrlPathHelper
();
...
...
@@ -91,7 +93,6 @@ public class DefaultRequestToViewNameTranslator implements RequestToViewNameTran
* Set the value that will replace '<code>/</code>' as the separator
* in the view name. The default behavior simply leaves '<code>/</code>'
* as the separator.
* @param separator the desired separator value
*/
public
void
setSeparator
(
String
separator
)
{
this
.
separator
=
separator
;
...
...
@@ -100,16 +101,22 @@ public class DefaultRequestToViewNameTranslator implements RequestToViewNameTran
/**
* Set whether or not leading slashes should be stripped from the URI when
* generating the view name. Default is "true".
* @param stripLeadingSlash <code>true</code> if leading slashes are to be stripped
*/
public
void
setStripLeadingSlash
(
boolean
stripLeadingSlash
)
{
this
.
stripLeadingSlash
=
stripLeadingSlash
;
}
/**
* Set whether or not trailing slashes should be stripped from the URI when
* generating the view name. Default is "true".
*/
public
void
setStripTrailingSlash
(
boolean
stripTrailingSlash
)
{
this
.
stripTrailingSlash
=
stripTrailingSlash
;
}
/**
* Set whether or not file extensions should be stripped from the URI when
* generating the view name. Default is "true".
* @param stripExtension <code>true</code> if file extensions should be stripped
*/
public
void
setStripExtension
(
boolean
stripExtension
)
{
this
.
stripExtension
=
stripExtension
;
...
...
@@ -120,7 +127,6 @@ public class DefaultRequestToViewNameTranslator implements RequestToViewNameTran
* context. Else, the path within the current servlet mapping is used
* if applicable (i.e. in the case of a ".../*" servlet mapping in web.xml).
* Default is "false".
* @param alwaysUseFullPath <code>true</code> if URL lookup should always use the full path
* @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath
*/
public
void
setAlwaysUseFullPath
(
boolean
alwaysUseFullPath
)
{
...
...
@@ -144,8 +150,6 @@ public class DefaultRequestToViewNameTranslator implements RequestToViewNameTran
* the resolution of lookup paths.
* <p>Use this to override the default UrlPathHelper with a custom subclass,
* or to share common UrlPathHelper settings across multiple web components.
* @param urlPathHelper the desired helper
* @throws IllegalArgumentException if the supplied UrlPathHelper is <code>null</code>
*/
public
void
setUrlPathHelper
(
UrlPathHelper
urlPathHelper
)
{
Assert
.
notNull
(
urlPathHelper
,
"UrlPathHelper must not be null"
);
...
...
@@ -177,6 +181,9 @@ public class DefaultRequestToViewNameTranslator implements RequestToViewNameTran
if
(
this
.
stripLeadingSlash
&&
path
.
startsWith
(
SLASH
))
{
path
=
path
.
substring
(
1
);
}
if
(
this
.
stripTrailingSlash
&&
path
.
endsWith
(
SLASH
))
{
path
=
path
.
substring
(
0
,
path
.
length
()
-
1
);
}
if
(
this
.
stripExtension
)
{
path
=
StringUtils
.
stripFilenameExtension
(
path
);
}
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/DefaultRequestToViewNameTranslatorTests.java
浏览文件 @
103ac035
/*
* Copyright 2002-20
06
the original author or authors.
* Copyright 2002-20
10
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.
...
...
@@ -16,15 +16,17 @@
package
org.springframework.web.servlet.view
;
import
junit.framework.TestCase
;
import
static
org
.
junit
.
Assert
.*;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.mock.web.MockHttpServletRequest
;
/**
* Unit tests for the DefaultRequestToViewNameTranslator class.
*
* @author Rick Evans
* @author Juergen Hoeller
*/
public
final
class
DefaultRequestToViewNameTranslatorTests
extends
TestCase
{
public
final
class
DefaultRequestToViewNameTranslatorTests
{
private
static
final
String
VIEW_NAME
=
"apple"
;
private
static
final
String
EXTENSION
=
".html"
;
...
...
@@ -34,68 +36,86 @@ public final class DefaultRequestToViewNameTranslatorTests extends TestCase {
private
MockHttpServletRequest
request
;
protected
void
setUp
()
throws
Exception
{
@Before
public
void
setUp
()
{
this
.
translator
=
new
DefaultRequestToViewNameTranslator
();
this
.
request
=
new
MockHttpServletRequest
();
this
.
request
.
setContextPath
(
CONTEXT_PATH
);
}
public
void
TODO_testGetViewNameLeavesLeadingSlashIfSoConfigured
()
throws
Exception
{
request
.
setRequestURI
(
CONTEXT_PATH
+
VIEW_NAME
);
@Test
public
void
testGetViewNameLeavesLeadingSlashIfSoConfigured
()
{
request
.
setRequestURI
(
CONTEXT_PATH
+
"/"
+
VIEW_NAME
+
"/"
);
this
.
translator
.
setStripLeadingSlash
(
false
);
assertViewName
(
"/"
+
VIEW_NAME
);
}
public
void
testGetViewNameLeavesExtensionIfSoConfigured
()
throws
Exception
{
request
.
setRequestURI
(
CONTEXT_PATH
+
VIEW_NAME
+
EXTENSION
);
@Test
public
void
testGetViewNameLeavesTrailingSlashIfSoConfigured
()
{
request
.
setRequestURI
(
CONTEXT_PATH
+
"/"
+
VIEW_NAME
+
"/"
);
this
.
translator
.
setStripTrailingSlash
(
false
);
assertViewName
(
VIEW_NAME
+
"/"
);
}
@Test
public
void
testGetViewNameLeavesExtensionIfSoConfigured
()
{
request
.
setRequestURI
(
CONTEXT_PATH
+
"/"
+
VIEW_NAME
+
EXTENSION
);
this
.
translator
.
setStripExtension
(
false
);
assertViewName
(
VIEW_NAME
+
EXTENSION
);
}
public
void
testGetViewNameWithDefaultConfiguration
()
throws
Exception
{
@Test
public
void
testGetViewNameWithDefaultConfiguration
()
{
request
.
setRequestURI
(
CONTEXT_PATH
+
VIEW_NAME
+
EXTENSION
);
assertViewName
(
VIEW_NAME
);
}
public
void
testGetViewNameWithCustomSeparator
()
throws
Exception
{
@Test
public
void
testGetViewNameWithCustomSeparator
()
{
request
.
setRequestURI
(
CONTEXT_PATH
+
VIEW_NAME
+
"/fiona"
+
EXTENSION
);
this
.
translator
.
setSeparator
(
"_"
);
assertViewName
(
VIEW_NAME
+
"_fiona"
);
}
public
void
testGetViewNameWithNoExtension
()
throws
Exception
{
@Test
public
void
testGetViewNameWithNoExtension
()
{
request
.
setRequestURI
(
CONTEXT_PATH
+
VIEW_NAME
);
assertViewName
(
VIEW_NAME
);
}
public
void
testGetViewNameWithPrefix
()
throws
Exception
{
@Test
public
void
testGetViewNameWithPrefix
()
{
final
String
prefix
=
"fiona_"
;
request
.
setRequestURI
(
CONTEXT_PATH
+
VIEW_NAME
);
this
.
translator
.
setPrefix
(
prefix
);
assertViewName
(
prefix
+
VIEW_NAME
);
}
public
void
testGetViewNameWithNullPrefix
()
throws
Exception
{
@Test
public
void
testGetViewNameWithNullPrefix
()
{
request
.
setRequestURI
(
CONTEXT_PATH
+
VIEW_NAME
);
this
.
translator
.
setPrefix
(
null
);
assertViewName
(
VIEW_NAME
);
}
public
void
testGetViewNameWithSuffix
()
throws
Exception
{
@Test
public
void
testGetViewNameWithSuffix
()
{
final
String
suffix
=
".fiona"
;
request
.
setRequestURI
(
CONTEXT_PATH
+
VIEW_NAME
);
this
.
translator
.
setSuffix
(
suffix
);
assertViewName
(
VIEW_NAME
+
suffix
);
}
public
void
testGetViewNameWithNullSuffix
()
throws
Exception
{
@Test
public
void
testGetViewNameWithNullSuffix
()
{
request
.
setRequestURI
(
CONTEXT_PATH
+
VIEW_NAME
);
this
.
translator
.
setSuffix
(
null
);
assertViewName
(
VIEW_NAME
);
}
public
void
testTrySetUrlPathHelperToNull
()
throws
Exception
{
@Test
public
void
testTrySetUrlPathHelperToNull
()
{
try
{
this
.
translator
.
setUrlPathHelper
(
null
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录