Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
50117dce
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,发现更多精彩内容 >>
提交
50117dce
编写于
4月 26, 2011
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SPR-6909 Include URI template vars in data binding
上级
5c27a042
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
136 addition
and
17 deletion
+136
-17
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMethodAdapter.java
...method/annotation/RequestMappingHandlerMethodAdapter.java
+9
-2
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletInitBinderMethodDataBinderFactory.java
.../annotation/ServletInitBinderMethodDataBinderFactory.java
+30
-2
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletInitBinderMethodDataBinderFactoryTests.java
...tation/ServletInitBinderMethodDataBinderFactoryTests.java
+77
-0
org.springframework.web/src/main/java/org/springframework/web/method/annotation/InitBinderMethodDataBinderFactory.java
.../method/annotation/InitBinderMethodDataBinderFactory.java
+4
-3
org.springframework.web/src/main/java/org/springframework/web/method/annotation/support/ModelAttributeMethodProcessor.java
...hod/annotation/support/ModelAttributeMethodProcessor.java
+10
-4
org.springframework.web/src/main/java/org/springframework/web/method/support/ModelAndViewContainer.java
...ngframework/web/method/support/ModelAndViewContainer.java
+2
-2
org.springframework.web/src/test/java/org/springframework/web/method/annotation/support/ModelAttributeMethodProcessorTests.java
...nnotation/support/ModelAttributeMethodProcessorTests.java
+4
-4
未找到文件。
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMethodAdapter.java
浏览文件 @
50117dce
...
...
@@ -42,6 +42,7 @@ import org.springframework.http.converter.StringHttpMessageConverter;
import
org.springframework.http.converter.xml.SourceHttpMessageConverter
;
import
org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter
;
import
org.springframework.util.ReflectionUtils.MethodFilter
;
import
org.springframework.validation.DataBinder
;
import
org.springframework.web.bind.WebDataBinder
;
import
org.springframework.web.bind.annotation.InitBinder
;
import
org.springframework.web.bind.annotation.ModelAttribute
;
...
...
@@ -257,13 +258,19 @@ public class RequestMappingHandlerMethodAdapter extends AbstractHandlerMethodAda
}
/**
* Specify a WebBindingInitializer which will apply pre-configured
* configuration to every DataBinder that this controller uses.
* Set a WebBindingInitializer to apply configure every DataBinder instance this controller uses.
*/
public
void
setWebBindingInitializer
(
WebBindingInitializer
webBindingInitializer
)
{
this
.
webBindingInitializer
=
webBindingInitializer
;
}
/**
* Return the WebBindingInitializer which applies pre-configured configuration to {@link DataBinder} instances.
*/
public
WebBindingInitializer
getWebBindingInitializer
()
{
return
webBindingInitializer
;
}
/**
* Specify the strategy to store session attributes with.
* <p>Default is {@link org.springframework.web.bind.support.DefaultSessionAttributeStore},
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletInitBinderMethodDataBinderFactory.java
浏览文件 @
50117dce
...
...
@@ -17,12 +17,17 @@
package
org.springframework.web.servlet.mvc.method.annotation
;
import
java.util.List
;
import
java.util.Map
;
import
org.springframework.beans.MutablePropertyValues
;
import
org.springframework.web.bind.ServletRequestDataBinder
;
import
org.springframework.web.bind.WebDataBinder
;
import
org.springframework.web.bind.support.WebBindingInitializer
;
import
org.springframework.web.context.request.RequestAttributes
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.method.annotation.InitBinderMethodDataBinderFactory
;
import
org.springframework.web.method.support.InvocableHandlerMethod
;
import
org.springframework.web.servlet.HandlerMapping
;
/**
* An {@link InitBinderMethodDataBinderFactory} that creates a {@link ServletRequestDataBinder}.
...
...
@@ -47,7 +52,30 @@ public class ServletInitBinderMethodDataBinderFactory extends InitBinderMethodDa
*/
@Override
protected
WebDataBinder
createBinderInstance
(
Object
target
,
String
objectName
)
{
return
new
ServletRequestDataBinder
(
target
,
objectName
);
return
new
ServletRequest
PathVar
DataBinder
(
target
,
objectName
);
}
}
/**
* Adds URI template variables to the map of request values used to do data binding.
*/
private
static
class
ServletRequestPathVarDataBinder
extends
ServletRequestDataBinder
{
public
ServletRequestPathVarDataBinder
(
Object
target
,
String
objectName
)
{
super
(
target
,
objectName
);
}
@SuppressWarnings
(
"unchecked"
)
@Override
protected
void
doBind
(
MutablePropertyValues
mpvs
)
{
RequestAttributes
requestAttrs
=
RequestContextHolder
.
getRequestAttributes
();
if
(
requestAttrs
!=
null
)
{
String
key
=
HandlerMapping
.
URI_TEMPLATE_VARIABLES_ATTRIBUTE
;
int
scope
=
RequestAttributes
.
SCOPE_REQUEST
;
Map
<
String
,
String
>
uriTemplateVars
=
(
Map
<
String
,
String
>)
requestAttrs
.
getAttribute
(
key
,
scope
);
mpvs
.
addPropertyValues
(
uriTemplateVars
);
}
super
.
doBind
(
mpvs
);
}
}
}
\ No newline at end of file
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletInitBinderMethodDataBinderFactoryTests.java
0 → 100644
浏览文件 @
50117dce
/*
* Copyright 2002-2011 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.web.servlet.mvc.method.annotation
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
java.util.HashMap
;
import
java.util.Map
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.beans.TestBean
;
import
org.springframework.mock.web.MockHttpServletRequest
;
import
org.springframework.web.bind.ServletRequestDataBinder
;
import
org.springframework.web.bind.WebDataBinder
;
import
org.springframework.web.context.request.NativeWebRequest
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletWebRequest
;
import
org.springframework.web.servlet.HandlerMapping
;
/**
* Test fixture with {@link ServletInitBinderMethodDataBinderFactory}.
*
* @author Rossen Stoyanchev
*/
public
class
ServletInitBinderMethodDataBinderFactoryTests
{
private
ServletInitBinderMethodDataBinderFactory
binderFactory
;
private
MockHttpServletRequest
request
;
private
NativeWebRequest
webRequest
;
@Before
public
void
setup
()
{
binderFactory
=
new
ServletInitBinderMethodDataBinderFactory
(
null
,
null
);
request
=
new
MockHttpServletRequest
();
webRequest
=
new
ServletWebRequest
(
request
);
RequestContextHolder
.
setRequestAttributes
(
webRequest
);
}
@After
public
void
teardown
()
{
RequestContextHolder
.
resetRequestAttributes
();
}
@Test
public
void
createBinder
()
throws
Exception
{
Map
<
String
,
String
>
uriTemplateVars
=
new
HashMap
<
String
,
String
>();
uriTemplateVars
.
put
(
"name"
,
"nameValue"
);
uriTemplateVars
.
put
(
"age"
,
"25"
);
request
.
setAttribute
(
HandlerMapping
.
URI_TEMPLATE_VARIABLES_ATTRIBUTE
,
uriTemplateVars
);
TestBean
target
=
new
TestBean
();
WebDataBinder
binder
=
binderFactory
.
createBinder
(
webRequest
,
target
,
""
);
((
ServletRequestDataBinder
)
binder
).
bind
(
request
);
assertEquals
(
"nameValue"
,
target
.
getName
());
assertEquals
(
25
,
target
.
getAge
());
}
}
org.springframework.web/src/main/java/org/springframework/web/method/annotation/InitBinderMethodDataBinderFactory.java
浏览文件 @
50117dce
...
...
@@ -16,6 +16,7 @@
package
org.springframework.web.method.annotation
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.HashSet
;
import
java.util.List
;
...
...
@@ -41,13 +42,13 @@ public class InitBinderMethodDataBinderFactory extends DefaultDataBinderFactory
/**
* Create an {@code InitBinderMethodDataBinderFactory} instance with the given {@link InitBinder} methods.
* @param
initB
inderMethods {@link InitBinder} methods to use to invoke to initialize new data binder instances
* @param
b
inderMethods {@link InitBinder} methods to use to invoke to initialize new data binder instances
* @param bindingInitializer a {@link WebBindingInitializer} to initialize new data binder instances with
*/
public
InitBinderMethodDataBinderFactory
(
List
<
InvocableHandlerMethod
>
initB
inderMethods
,
public
InitBinderMethodDataBinderFactory
(
List
<
InvocableHandlerMethod
>
b
inderMethods
,
WebBindingInitializer
bindingInitializer
)
{
super
(
bindingInitializer
);
this
.
initBinderMethods
=
initBinderMethods
;
this
.
initBinderMethods
=
(
binderMethods
!=
null
)
?
binderMethods
:
new
ArrayList
<
InvocableHandlerMethod
>()
;
}
/**
...
...
org.springframework.web/src/main/java/org/springframework/web/method/annotation/support/ModelAttributeMethodProcessor.java
浏览文件 @
50117dce
...
...
@@ -94,11 +94,11 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
if
(
binder
.
getTarget
()
!=
null
)
{
doBind
(
binder
,
webRequest
);
if
(
shouldValidate
(
parameter
))
{
if
(
shouldValidate
(
binder
,
parameter
))
{
binder
.
validate
();
}
if
(
failOnError
(
parameter
)
&&
binder
.
getBindingResult
().
hasErrors
())
{
if
(
failOnError
(
binder
,
parameter
)
&&
binder
.
getBindingResult
().
hasErrors
())
{
throw
new
BindException
(
binder
.
getBindingResult
());
}
}
...
...
@@ -133,9 +133,12 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
}
/**
* Whether to validate the target object of the given {@link WebDataBinder} instance.
* @param binder the data binder containing the validation candidate
* @param parameter the method argument for which data binding is performed
* @return true if {@link DataBinder#validate()} should be invoked, false otherwise.
*/
protected
boolean
shouldValidate
(
MethodParameter
parameter
)
{
protected
boolean
shouldValidate
(
WebDataBinder
binder
,
MethodParameter
parameter
)
{
Annotation
[]
annotations
=
parameter
.
getParameterAnnotations
();
for
(
Annotation
annot
:
annotations
)
{
if
(
"Valid"
.
equals
(
annot
.
annotationType
().
getSimpleName
()))
{
...
...
@@ -146,9 +149,12 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
}
/**
* Whether to raise a {@link BindException} in case of data binding or validation errors.
* @param binder the binder on which validation is to be invoked
* @param parameter the method argument for which data binding is performed
* @return true if the binding or validation errors should result in a {@link BindException}, false otherwise.
*/
protected
boolean
failOnError
(
MethodParameter
parameter
)
{
protected
boolean
failOnError
(
WebDataBinder
binder
,
MethodParameter
parameter
)
{
int
i
=
parameter
.
getParameterIndex
();
Class
<?>[]
paramTypes
=
parameter
.
getMethod
().
getParameterTypes
();
boolean
hasBindingResult
=
(
paramTypes
.
length
>
(
i
+
1
)
&&
Errors
.
class
.
isAssignableFrom
(
paramTypes
[
i
+
1
]));
...
...
org.springframework.web/src/main/java/org/springframework/web/method/support/ModelAndViewContainer.java
浏览文件 @
50117dce
...
...
@@ -24,8 +24,8 @@ import org.springframework.util.StringUtils;
import
org.springframework.validation.support.BindingAwareModelMap
;
/**
* Provides access to the model and a place to record model and view related decisions
to all
* {@link HandlerMethodArgumentResolver}s
and {@link HandlerMethodReturnValueHandler}s
.
* Provides access to the model and a place to record model and view related decisions
made by
* {@link HandlerMethodArgumentResolver}s
or a {@link HandlerMethodReturnValueHandler}
.
*
* <p>In addition to storing model attributes and a view, the {@link ModelAndViewContainer} also provides
* a {@link #setResolveView(boolean)} flag, which can be used to request or bypass a view resolution phase.
...
...
org.springframework.web/src/test/java/org/springframework/web/method/annotation/support/ModelAttributeMethodProcessorTests.java
浏览文件 @
50117dce
...
...
@@ -132,14 +132,14 @@ public class ModelAttributeMethodProcessorTests {
@Test
public
void
shouldValidate
()
throws
Exception
{
assertTrue
(
processor
.
shouldValidate
(
paramNamedValidModelAttr
));
assertFalse
(
processor
.
shouldValidate
(
paramNonSimpleType
));
assertTrue
(
processor
.
shouldValidate
(
null
,
paramNamedValidModelAttr
));
assertFalse
(
processor
.
shouldValidate
(
null
,
paramNonSimpleType
));
}
@Test
public
void
failOnError
()
throws
Exception
{
assertFalse
(
"Shouldn't failOnError with BindingResult"
,
processor
.
failOnError
(
paramNamedValidModelAttr
));
assertTrue
(
"Should failOnError without BindingResult"
,
processor
.
failOnError
(
paramNonSimpleType
));
assertFalse
(
"Shouldn't failOnError with BindingResult"
,
processor
.
failOnError
(
null
,
paramNamedValidModelAttr
));
assertTrue
(
"Should failOnError without BindingResult"
,
processor
.
failOnError
(
null
,
paramNonSimpleType
));
}
@Test
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录