Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
sesemomo
spring-framework
提交
172afb51
S
spring-framework
项目概览
sesemomo
/
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,发现更多精彩内容 >>
提交
172afb51
编写于
5月 16, 2019
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Polish AbstractView in WebFlux
上级
ffd7cffa
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
38 addition
and
41 deletion
+38
-41
spring-webflux/src/main/java/org/springframework/web/reactive/result/view/AbstractView.java
...pringframework/web/reactive/result/view/AbstractView.java
+34
-38
spring-webflux/src/test/java/org/springframework/web/reactive/result/view/AbstractViewTests.java
...framework/web/reactive/result/view/AbstractViewTests.java
+4
-3
未找到文件。
spring-webflux/src/main/java/org/springframework/web/reactive/result/view/AbstractView.java
浏览文件 @
172afb51
...
...
@@ -20,9 +20,9 @@ import java.nio.charset.Charset;
import
java.nio.charset.StandardCharsets
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
...
...
@@ -211,30 +211,27 @@ public abstract class AbstractView implements View, BeanNameAware, ApplicationCo
* <p>The default implementation creates a combined output Map that includes
* model as well as static attributes with the former taking precedence.
*/
protected
Mono
<
Map
<
String
,
Object
>>
getModelAttributes
(
@Nullable
Map
<
String
,
?>
model
,
ServerWebExchange
exchange
)
{
protected
Mono
<
Map
<
String
,
Object
>>
getModelAttributes
(
@Nullable
Map
<
String
,
?>
model
,
ServerWebExchange
exchange
)
{
int
size
=
(
model
!=
null
?
model
.
size
()
:
0
);
Map
<
String
,
Object
>
attributes
=
new
LinkedHashMap
<>(
size
);
Map
<
String
,
Object
>
attributes
=
new
ConcurrentHashMap
<>(
size
);
if
(
model
!=
null
)
{
attributes
.
putAll
(
model
);
}
return
resolveAsyncAttributes
(
attributes
).
then
(
Mono
.
just
(
attributes
));
return
resolveAsyncAttributes
(
attributes
).
thenReturn
(
attributes
);
}
/**
* By default, resolve async attributes supported by the
* {@link ReactiveAdapterRegistry} to their blocking counterparts.
* <p>View implementations capable of taking advantage of reactive types
* can override this method if needed.
* @return {@code Mono} for the completion of async attributes resolution
* Use the configured {@link ReactiveAdapterRegistry} to adapt asynchronous
* model attributes to {@code Mono<T>} or {@code Mono<List<T>>} and resolve
* them to actual values via {@link Mono#zip(Mono, Mono)}, so that when
* the returned result {@code Mono} completes, the model has its asynchronous
* attributes replaced with synchronous values.
* @return result {@code Mono} that completes when the model is ready
*/
protected
Mono
<
Void
>
resolveAsyncAttributes
(
Map
<
String
,
Object
>
model
)
{
List
<
String
>
names
=
new
ArrayList
<>();
List
<
Mono
<?>>
valueMonos
=
new
ArrayList
<>();
List
<
Mono
<?>>
asyncAttributes
=
null
;
for
(
Map
.
Entry
<
String
,
?>
entry
:
model
.
entrySet
())
{
Object
value
=
entry
.
getValue
();
if
(
value
==
null
)
{
...
...
@@ -242,35 +239,34 @@ public abstract class AbstractView implements View, BeanNameAware, ApplicationCo
}
ReactiveAdapter
adapter
=
this
.
adapterRegistry
.
getAdapter
(
null
,
value
);
if
(
adapter
!=
null
)
{
names
.
add
(
entry
.
getKey
());
if
(
asyncAttributes
==
null
)
{
asyncAttributes
=
new
ArrayList
<>();
}
String
name
=
entry
.
getKey
();
if
(
adapter
.
isMultiValue
())
{
Flux
<
Object
>
fluxValue
=
Flux
.
from
(
adapter
.
toPublisher
(
value
));
valueMonos
.
add
(
fluxValue
.
collectList
().
defaultIfEmpty
(
Collections
.
emptyList
()));
asyncAttributes
.
add
(
Flux
.
from
(
adapter
.
toPublisher
(
value
))
.
collectList
()
.
doOnSuccess
(
result
->
{
result
=
result
!=
null
?
result
:
Collections
.
emptyList
();
model
.
put
(
name
,
result
);
}));
}
else
{
Mono
<
Object
>
monoValue
=
Mono
.
from
(
adapter
.
toPublisher
(
value
));
valueMonos
.
add
(
monoValue
.
defaultIfEmpty
(
NO_VALUE
));
asyncAttributes
.
add
(
Mono
.
from
(
adapter
.
toPublisher
(
value
))
.
doOnSuccess
(
result
->
{
if
(
result
!=
null
)
{
model
.
put
(
name
,
result
);
}
else
{
model
.
remove
(
name
);
}
}));
}
}
}
if
(
names
.
isEmpty
())
{
return
Mono
.
empty
();
}
return
Mono
.
zip
(
valueMonos
,
values
->
{
for
(
int
i
=
0
;
i
<
values
.
length
;
i
++)
{
if
(
values
[
i
]
!=
NO_VALUE
)
{
model
.
put
(
names
.
get
(
i
),
values
[
i
]);
}
else
{
model
.
remove
(
names
.
get
(
i
));
}
}
return
NO_VALUE
;
})
.
then
();
return
asyncAttributes
!=
null
?
Mono
.
when
(
asyncAttributes
)
:
Mono
.
empty
();
}
/**
...
...
spring-webflux/src/test/java/org/springframework/web/reactive/result/view/AbstractViewTests.java
浏览文件 @
172afb51
/*
* Copyright 2002-201
8
the original author or authors.
* Copyright 2002-201
9
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,6 +16,7 @@
package
org.springframework.web.reactive.result.view
;
import
java.time.Duration
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
...
...
@@ -54,8 +55,8 @@ public class AbstractViewTests {
TestBean
testBean1
=
new
TestBean
(
"Bean1"
);
TestBean
testBean2
=
new
TestBean
(
"Bean2"
);
Map
<
String
,
Object
>
attributes
=
new
HashMap
<>();
attributes
.
put
(
"attr1"
,
Mono
.
just
(
testBean1
));
attributes
.
put
(
"attr2"
,
Flux
.
just
(
testBean1
,
testBean2
));
attributes
.
put
(
"attr1"
,
Mono
.
just
(
testBean1
)
.
delayElement
(
Duration
.
ofMillis
(
10
))
);
attributes
.
put
(
"attr2"
,
Flux
.
just
(
testBean1
,
testBean2
)
.
delayElements
(
Duration
.
ofMillis
(
10
))
);
attributes
.
put
(
"attr3"
,
Single
.
just
(
testBean2
));
attributes
.
put
(
"attr4"
,
Observable
.
just
(
testBean1
,
testBean2
));
attributes
.
put
(
"attr5"
,
Mono
.
empty
());
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录