Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
ed9d9a40
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,发现更多精彩内容 >>
提交
ed9d9a40
编写于
4月 21, 2011
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SPR-7543 Add @PathVariables to the model
上级
a4716c2a
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
46 addition
and
11 deletion
+46
-11
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/PathVariableMethodArgumentResolver.java
...nnotation/support/PathVariableMethodArgumentResolver.java
+12
-0
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMethodAdapterIntegrationTests.java
...n/RequestMappingHandlerMethodAdapterIntegrationTests.java
+1
-1
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/PathVariableMethodArgumentResolverTests.java
...tion/support/PathVariableMethodArgumentResolverTests.java
+9
-4
org.springframework.web/src/main/java/org/springframework/web/method/annotation/support/AbstractNamedValueMethodArgumentResolver.java
...ion/support/AbstractNamedValueMethodArgumentResolver.java
+24
-6
未找到文件。
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/PathVariableMethodArgumentResolver.java
浏览文件 @
ed9d9a40
...
...
@@ -27,6 +27,7 @@ import org.springframework.web.bind.annotation.ValueConstants;
import
org.springframework.web.context.request.NativeWebRequest
;
import
org.springframework.web.context.request.RequestAttributes
;
import
org.springframework.web.method.annotation.support.AbstractNamedValueMethodArgumentResolver
;
import
org.springframework.web.method.support.ModelAndViewContainer
;
import
org.springframework.web.servlet.HandlerMapping
;
/**
...
...
@@ -73,6 +74,17 @@ public class PathVariableMethodArgumentResolver extends AbstractNamedValueMethod
throw
new
IllegalStateException
(
"Could not find the URL template variable ["
+
name
+
"]"
);
}
@Override
protected
void
handleResolvedValue
(
Object
arg
,
String
name
,
MethodParameter
parameter
,
ModelAndViewContainer
mavContainer
,
NativeWebRequest
webRequest
)
{
if
(
mavContainer
!=
null
)
{
mavContainer
.
addAttribute
(
name
,
arg
);
}
}
private
static
class
PathVariableNamedValueInfo
extends
NamedValueInfo
{
private
PathVariableNamedValueInfo
(
PathVariable
annotation
)
{
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMethodAdapterIntegrationTests.java
浏览文件 @
ed9d9a40
...
...
@@ -293,7 +293,7 @@ public class RequestMappingHandlerMethodAdapterIntegrationTests {
@ModelAttribute
OtherUser
otherUser
,
Model
model
)
throws
Exception
{
model
.
addAttribute
(
"cookie"
,
cookie
).
addAttribute
(
"
pathvar"
,
pathvar
).
addAttribute
(
"
header"
,
header
)
model
.
addAttribute
(
"cookie"
,
cookie
).
addAttribute
(
"header"
,
header
)
.
addAttribute
(
"systemHeader"
,
systemHeader
).
addAttribute
(
"headerMap"
,
headerMap
)
.
addAttribute
(
"dateParam"
,
dateParam
).
addAttribute
(
"paramMap"
,
paramMap
)
.
addAttribute
(
"paramByConvention"
,
paramByConvention
).
addAttribute
(
"value"
,
value
)
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/PathVariableMethodArgumentResolverTests.java
浏览文件 @
ed9d9a40
...
...
@@ -32,6 +32,7 @@ import org.springframework.mock.web.MockHttpServletRequest;
import
org.springframework.mock.web.MockHttpServletResponse
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.context.request.ServletWebRequest
;
import
org.springframework.web.method.support.ModelAndViewContainer
;
import
org.springframework.web.servlet.HandlerMapping
;
import
org.springframework.web.servlet.mvc.method.annotation.support.PathVariableMethodArgumentResolver
;
...
...
@@ -48,6 +49,8 @@ public class PathVariableMethodArgumentResolverTests {
private
MethodParameter
paramString
;
private
ModelAndViewContainer
mavContainer
;
private
ServletWebRequest
webRequest
;
private
MockHttpServletRequest
request
;
...
...
@@ -60,6 +63,7 @@ public class PathVariableMethodArgumentResolverTests {
paramNamedString
=
new
MethodParameter
(
method
,
0
);
paramString
=
new
MethodParameter
(
method
,
1
);
mavContainer
=
new
ModelAndViewContainer
();
request
=
new
MockHttpServletRequest
();
webRequest
=
new
ServletWebRequest
(
request
,
new
MockHttpServletResponse
());
}
...
...
@@ -76,13 +80,14 @@ public class PathVariableMethodArgumentResolverTests {
uriTemplateVars
.
put
(
"name"
,
"value"
);
request
.
setAttribute
(
HandlerMapping
.
URI_TEMPLATE_VARIABLES_ATTRIBUTE
,
uriTemplateVars
);
String
result
=
(
String
)
resolver
.
resolveArgument
(
paramNamedString
,
null
,
webRequest
,
null
);
assertEquals
(
"value"
,
result
);
String
result
=
(
String
)
resolver
.
resolveArgument
(
paramNamedString
,
mavContainer
,
webRequest
,
null
);
assertEquals
(
"PathVariable not resolved correctly"
,
"value"
,
result
);
assertEquals
(
"PathVariable not added to the model"
,
"value"
,
mavContainer
.
getAttribute
(
"name"
));
}
@Test
(
expected
=
IllegalStateException
.
class
)
public
void
handleMissingValue
()
throws
Exception
{
resolver
.
resolveArgument
(
paramNamedString
,
null
,
webRequest
,
null
);
resolver
.
resolveArgument
(
paramNamedString
,
mavContainer
,
webRequest
,
null
);
fail
(
"Unresolved path variable should lead to exception."
);
}
...
...
org.springframework.web/src/main/java/org/springframework/web/method/annotation/support/AbstractNamedValueMethodArgumentResolver.java
浏览文件 @
ed9d9a40
...
...
@@ -42,6 +42,7 @@ import org.springframework.web.method.support.ModelAndViewContainer;
* <li>Obtain named value information for a method parameter
* <li>Resolve names into argument values
* <li>Handle missing argument values when argument values are required
* <li>Optionally handle a resolved value
* </ul>
* <p>A default value string can contain ${...} placeholders and Spring Expression Language #{...} expressions.
* For this to work a {@link ConfigurableBeanFactory} must be supplied to the class constructor.
...
...
@@ -87,16 +88,17 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
else
if
(
namedValueInfo
.
required
)
{
handleMissingValue
(
namedValueInfo
.
name
,
parameter
);
}
arg
=
checkForNull
(
namedValueInfo
.
name
,
arg
,
paramType
);
arg
=
handleNullValue
(
namedValueInfo
.
name
,
arg
,
paramType
);
}
if
(
binderFactory
!=
null
)
{
WebDataBinder
binder
=
binderFactory
.
createBinder
(
webRequest
,
null
,
namedValueInfo
.
name
);
return
binder
.
convertIfNecessary
(
arg
,
paramType
,
parameter
);
}
else
{
return
arg
;
arg
=
binder
.
convertIfNecessary
(
arg
,
paramType
,
parameter
);
}
handleResolvedValue
(
arg
,
namedValueInfo
.
name
,
parameter
,
mavContainer
,
webRequest
);
return
arg
;
}
/**
...
...
@@ -170,7 +172,10 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
*/
protected
abstract
void
handleMissingValue
(
String
name
,
MethodParameter
parameter
)
throws
ServletException
;
private
Object
checkForNull
(
String
name
,
Object
value
,
Class
<?>
paramType
)
{
/**
* A {@code null} results in a {@code false} value for {@code boolean}s or an exception for other primitives.
*/
private
Object
handleNullValue
(
String
name
,
Object
value
,
Class
<?>
paramType
)
{
if
(
value
==
null
)
{
if
(
Boolean
.
TYPE
.
equals
(
paramType
))
{
return
Boolean
.
FALSE
;
...
...
@@ -184,6 +189,19 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
return
value
;
}
/**
* Invoked after a value is resolved.
* @param arg the resolved argument value
* @param name the argument name
* @param parameter the argument parameter type
* @param mavContainer the {@link ModelAndViewContainer}, which may be {@code null}
* @param webRequest the current request
*/
protected
void
handleResolvedValue
(
Object
arg
,
String
name
,
MethodParameter
parameter
,
ModelAndViewContainer
mavContainer
,
NativeWebRequest
webRequest
)
{
}
/**
* Represents the information about a named value, including name, whether it's required and a default value.
*/
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录