Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
2288b252
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,发现更多精彩内容 >>
提交
2288b252
编写于
10月 29, 2009
作者:
A
Arjen Poutsma
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SPR-6021 - Allow for using MultiValueMap in GET request for mapping multiple request params
上级
53eb612a
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
85 addition
and
0 deletion
+85
-0
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java
...vlet/mvc/annotation/ServletAnnotationControllerTests.java
+56
-0
org.springframework.web/src/main/java/org/springframework/web/bind/annotation/support/HandlerMethodInvoker.java
...ork/web/bind/annotation/support/HandlerMethodInvoker.java
+29
-0
未找到文件。
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java
浏览文件 @
2288b252
...
...
@@ -35,6 +35,7 @@ import java.util.List;
import
java.util.Locale
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.Iterator
;
import
javax.servlet.ServletConfig
;
import
javax.servlet.ServletContext
;
import
javax.servlet.ServletException
;
...
...
@@ -80,6 +81,7 @@ import org.springframework.ui.format.date.DateFormatter;
import
org.springframework.ui.format.support.GenericFormatterRegistry
;
import
org.springframework.util.SerializationTestUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.validation.Errors
;
import
org.springframework.validation.FieldError
;
...
...
@@ -1146,6 +1148,25 @@ public class ServletAnnotationControllerTests {
}
@Test
public
void
requestParamMap
()
throws
Exception
{
initServlet
(
RequestParamMapController
.
class
);
MockHttpServletRequest
request
=
new
MockHttpServletRequest
(
"GET"
,
"/map"
);
request
.
addParameter
(
"key1"
,
"value1"
);
request
.
addParameter
(
"key2"
,
new
String
[]{
"value21"
,
"value22"
});
MockHttpServletResponse
response
=
new
MockHttpServletResponse
();
servlet
.
service
(
request
,
response
);
assertEquals
(
"key1=value1,key2=value21"
,
response
.
getContentAsString
());
request
.
setRequestURI
(
"/multiValueMap"
);
response
=
new
MockHttpServletResponse
();
servlet
.
service
(
request
,
response
);
assertEquals
(
"key1=[value1],key2=[value21,value22]"
,
response
.
getContentAsString
());
}
/*
* Controllers
...
...
@@ -1942,6 +1963,41 @@ public class ServletAnnotationControllerTests {
return
new
ModelAndView
(
"/something"
);
}
}
@Controller
public
static
class
RequestParamMapController
{
@RequestMapping
(
"/map"
)
public
void
map
(
@RequestParam
Map
<
String
,
String
>
params
,
Writer
writer
)
throws
IOException
{
for
(
Iterator
<
Map
.
Entry
<
String
,
String
>>
it
=
params
.
entrySet
().
iterator
();
it
.
hasNext
();)
{
Map
.
Entry
<
String
,
String
>
entry
=
it
.
next
();
writer
.
write
(
entry
.
getKey
()
+
"="
+
entry
.
getValue
());
if
(
it
.
hasNext
())
{
writer
.
write
(
','
);
}
}
}
@RequestMapping
(
"/multiValueMap"
)
public
void
multiValueMap
(
@RequestParam
MultiValueMap
<
String
,
String
>
params
,
Writer
writer
)
throws
IOException
{
for
(
Iterator
<
Map
.
Entry
<
String
,
List
<
String
>>>
it1
=
params
.
entrySet
().
iterator
();
it1
.
hasNext
();)
{
Map
.
Entry
<
String
,
List
<
String
>>
entry
=
it1
.
next
();
writer
.
write
(
entry
.
getKey
()
+
"=["
);
for
(
Iterator
<
String
>
it2
=
entry
.
getValue
().
iterator
();
it2
.
hasNext
();)
{
String
value
=
it2
.
next
();
writer
.
write
(
value
);
if
(
it2
.
hasNext
())
{
writer
.
write
(
','
);
}
}
writer
.
write
(
']'
);
if
(
it1
.
hasNext
())
{
writer
.
write
(
','
);
}
}
}
}
}
org.springframework.web/src/main/java/org/springframework/web/bind/annotation/support/HandlerMethodInvoker.java
浏览文件 @
2288b252
...
...
@@ -26,6 +26,7 @@ import java.util.HashSet;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.LinkedHashMap
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
...
...
@@ -46,6 +47,8 @@ import org.springframework.ui.Model;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.ReflectionUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.validation.Errors
;
import
org.springframework.web.HttpMediaTypeNotSupportedException
;
...
...
@@ -396,11 +399,15 @@ public class HandlerMethodInvoker {
return
initBinderArgs
;
}
@SuppressWarnings
(
"unchecked"
)
private
Object
resolveRequestParam
(
String
paramName
,
boolean
required
,
String
defaultValue
,
MethodParameter
methodParam
,
NativeWebRequest
webRequest
,
Object
handlerForInitBinderCall
)
throws
Exception
{
Class
<?>
paramType
=
methodParam
.
getParameterType
();
if
(
Map
.
class
.
isAssignableFrom
(
paramType
))
{
return
resolveRequestParamMap
((
Class
<?
extends
Map
>)
paramType
,
webRequest
);
}
if
(
paramName
.
length
()
==
0
)
{
paramName
=
getRequiredParameterName
(
methodParam
);
}
...
...
@@ -428,6 +435,28 @@ public class HandlerMethodInvoker {
return
binder
.
convertIfNecessary
(
paramValue
,
paramType
,
methodParam
);
}
private
Map
resolveRequestParamMap
(
Class
<?
extends
Map
>
mapType
,
NativeWebRequest
webRequest
)
{
Map
<
String
,
String
[]>
parameterMap
=
webRequest
.
getParameterMap
();
if
(
MultiValueMap
.
class
.
isAssignableFrom
(
mapType
))
{
MultiValueMap
<
String
,
String
>
result
=
new
LinkedMultiValueMap
<
String
,
String
>(
parameterMap
.
size
());
for
(
Map
.
Entry
<
String
,
String
[]>
entry
:
parameterMap
.
entrySet
())
{
for
(
String
value
:
entry
.
getValue
())
{
result
.
add
(
entry
.
getKey
(),
value
);
}
}
return
result
;
}
else
{
Map
<
String
,
String
>
result
=
new
LinkedHashMap
<
String
,
String
>(
parameterMap
.
size
());
for
(
Map
.
Entry
<
String
,
String
[]>
entry
:
parameterMap
.
entrySet
())
{
if
(
entry
.
getValue
().
length
>
0
)
{
result
.
put
(
entry
.
getKey
(),
entry
.
getValue
()[
0
]);
}
}
return
result
;
}
}
private
Object
resolveRequestHeader
(
String
headerName
,
boolean
required
,
String
defaultValue
,
MethodParameter
methodParam
,
NativeWebRequest
webRequest
,
Object
handlerForInitBinderCall
)
throws
Exception
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录