Skip to content

Commit aa6b47b

Browse files
committed
Polishing in FragmentsRendering
See gh-33194
1 parent 7c9bb24 commit aa6b47b

File tree

3 files changed

+45
-42
lines changed

3 files changed

+45
-42
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/result/view/FragmentsRendering.java

+20-13
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
import org.springframework.util.Assert;
3333

3434
/**
35-
* Public API for HTML rendering from a collection or from a stream of
36-
* {@link Fragment}s each with its own view and model. For use with
37-
* view technologies such as <a href="https://htmx.org/">htmx</a> where multiple
38-
* page fragments may be rendered in a single response. Supported as a return
39-
* value from a WebFlux controller method.
35+
* Public API for HTML rendering of a collection of fragments each with a view
36+
* and independent model. For use with frontends technologies such as
37+
* <a href="https://htmx.org/">htmx</a> where multiple page fragments may be
38+
* rendered in one response. Supported as a return value from Spring WebFlux
39+
* controller methods.
4040
*
4141
* <p>For full page rendering with a single model and view, use {@link Rendering}.
4242
*
@@ -74,8 +74,8 @@ static Builder with(String viewName, Map<String, Object> model) {
7474
}
7575

7676
/**
77-
* Create a builder and add a fragment with a view name only, also
78-
* inheriting model attributes from the model for the request.
77+
* Variant of {@link #with(String, Map)} with a view name only, but also
78+
* inheriting model attributes from the shared model for the request.
7979
* @param viewName the name of the view for the fragment
8080
* @return this builder
8181
*/
@@ -84,14 +84,20 @@ static Builder with(String viewName) {
8484
}
8585

8686
/**
87-
* Create a builder to render with a collection of Fragments.
87+
* Variant of {@link #with(String, Map)} with a collection of fragments.
88+
* @param fragments the fragments to add; each fragment also inherits model
89+
* attributes from the shared model for the request
90+
* @return the created builder
8891
*/
8992
static Builder withCollection(Collection<Fragment> fragments) {
9093
return new DefaultFragmentsRenderingBuilder(fragments);
9194
}
9295

9396
/**
94-
* Create a builder to render with a {@link Publisher} of Fragments.
97+
* Variant of {@link #with(String, Map)} with a {@link Publisher} of fragments.
98+
* @param fragmentsPublisher the fragments to add; each fragment also
99+
* inherits model attributes from the shared model for the request
100+
* @return the created builder
95101
*/
96102
static <P extends Publisher<Fragment>> Builder withPublisher(P fragmentsPublisher) {
97103
return new DefaultFragmentsRenderingBuilder(fragmentsPublisher);
@@ -126,7 +132,7 @@ interface Builder {
126132
Builder status(HttpStatusCode status);
127133

128134
/**
129-
* Add the given, single header value under the given name.
135+
* Add one or more header values under the given name.
130136
* @param headerName the header name
131137
* @param headerValues the header value(s)
132138
* @return this builder
@@ -151,15 +157,16 @@ interface Builder {
151157
Builder fragment(String viewName, Map<String, Object> model);
152158

153159
/**
154-
* Add a fragment with a view name only, inheriting model attributes from
155-
* the model for the request.
160+
* Variant of {@link #fragment(String, Map)} with a view name only, where
161+
* the fragment model also inherits model attributes from the shared
162+
* model for the request.
156163
* @param viewName the name of the view for the fragment
157164
* @return this builder
158165
*/
159166
Builder fragment(String viewName);
160167

161168
/**
162-
* Add a fragment.
169+
* Variant of {@link #fragment(String, Map)} with a {@link Fragment}.
163170
* @param fragment the fragment to add
164171
* @return this builder
165172
*/

spring-webflux/src/test/java/org/springframework/web/reactive/result/view/FragmentViewResolutionResultHandlerTests.java

+15-18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Map;
2323
import java.util.stream.Stream;
2424

25+
import org.junit.jupiter.api.Test;
2526
import org.junit.jupiter.params.ParameterizedTest;
2627
import org.junit.jupiter.params.provider.Arguments;
2728
import org.junit.jupiter.params.provider.MethodSource;
@@ -54,25 +55,21 @@
5455
*/
5556
public class FragmentViewResolutionResultHandlerTests {
5657

58+
private static final Fragment fragment1 = Fragment.create("fragment1", Map.of("foo", "Foo"));
59+
60+
private static final Fragment fragment2 = Fragment.create("fragment2", Map.of("bar", "Bar"));
61+
62+
5763
static Stream<Arguments> arguments() {
58-
Fragment f1 = Fragment.create("fragment1", Map.of("foo", "Foo"));
59-
Fragment f2 = Fragment.create("fragment2", Map.of("bar", "Bar"));
64+
Flux<Fragment> fragmentFlux = Flux.just(fragment1, fragment2).subscribeOn(Schedulers.boundedElastic());
6065
return Stream.of(
61-
Arguments.of(
62-
FragmentsRendering.withPublisher(Flux.just(f1, f2).subscribeOn(Schedulers.boundedElastic()))
63-
.headers(headers -> headers.setContentType(MediaType.TEXT_HTML))
64-
.build(),
66+
Arguments.of(FragmentsRendering.withPublisher(fragmentFlux).build(),
6567
on(Handler.class).resolveReturnType(FragmentsRendering.class)),
66-
Arguments.of(
67-
FragmentsRendering.withCollection(List.of(f1, f2))
68-
.headers(headers -> headers.setContentType(MediaType.TEXT_HTML))
69-
.build(),
68+
Arguments.of(FragmentsRendering.withCollection(List.of(fragment1, fragment2)).build(),
7069
on(Handler.class).resolveReturnType(FragmentsRendering.class)),
71-
Arguments.of(
72-
Flux.just(f1, f2).subscribeOn(Schedulers.boundedElastic()),
70+
Arguments.of(fragmentFlux,
7371
on(Handler.class).resolveReturnType(Flux.class, Fragment.class)),
74-
Arguments.of(
75-
List.of(f1, f2),
72+
Arguments.of(List.of(fragment1, fragment2),
7673
on(Handler.class).resolveReturnType(List.class, Fragment.class)));
7774
}
7875

@@ -107,14 +104,14 @@ private ViewResolutionResultHandler initHandler() {
107104
}
108105

109106

110-
@SuppressWarnings("unused")
107+
@SuppressWarnings({"unused", "DataFlowIssue"})
111108
private static class Handler {
112109

113-
FragmentsRendering rendering() { return null; }
110+
FragmentsRendering render() { return null; }
114111

115-
Flux<Fragment> fragmentFlux() { return null; }
112+
Flux<Fragment> renderFlux() { return null; }
116113

117-
List<Fragment> fragmentList() { return null; }
114+
List<Fragment> renderList() { return null; }
118115

119116
}
120117

spring-webmvc/src/main/java/org/springframework/web/servlet/view/FragmentsRendering.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
import org.springframework.web.servlet.SmartView;
2828

2929
/**
30-
* Public API for HTML rendering a collection fragments each with its own view
31-
* and model. For use with view technologies such as
30+
* Public API for HTML rendering of a collection of fragments each with a view
31+
* and independent model. For use with frontends technologies such as
3232
* <a href="https://htmx.org/">htmx</a> where multiple page fragments may be
33-
* rendered in a single response. Supported as a return value from a Spring MVC
34-
* controller method.
33+
* rendered in one response. Supported as a return value from Spring MVC
34+
* controller methods.
3535
*
3636
* @author Rossen Stoyanchev
3737
* @since 6.2
@@ -51,8 +51,7 @@ public interface FragmentsRendering extends SmartView {
5151

5252

5353
/**
54-
* Create a builder for {@link FragmentsRendering}, adding a fragment with
55-
* the given view name and model.
54+
* Create a builder and add a fragment with a view name and a model.
5655
* @param viewName the name of the view for the fragment
5756
* @param model attributes for the fragment in addition to model
5857
* attributes inherited from the shared model for the request
@@ -96,7 +95,7 @@ interface Builder {
9695
Builder status(HttpStatusCode status);
9796

9897
/**
99-
* Add the given, single header value under the given name.
98+
* Add one or more header values under the given name.
10099
* @param headerName the header name
101100
* @param headerValues the header value(s)
102101
* @return this builder
@@ -121,23 +120,23 @@ interface Builder {
121120
Builder fragment(String viewName, Map<String, Object> model);
122121

123122
/**
124-
* Add a fragment with a view name only, inheriting model attributes from
125-
* the model for the request.
123+
* Variant of {@link #fragment(String, Map)} with a view name only, but
124+
* also inheriting model attributes from the shared model for the request.
126125
* @param viewName the name of the view for the fragment
127126
* @return this builder
128127
*/
129128
Builder fragment(String viewName);
130129

131130
/**
132-
* Add a fragment.
131+
* Variant of {@link #fragment(String, Map)} with a {@link ModelAndView}.
133132
* @param fragment the fragment to add; the fragment also inherits model
134133
* attributes from the shared model for the request
135134
* @return this builder
136135
*/
137136
Builder fragment(ModelAndView fragment);
138137

139138
/**
140-
* Add a collection of fragments.
139+
* Variant of {@link #fragment(String, Map)} with a collection of {@link ModelAndView}s.
141140
* @param fragments the fragments to add; each fragment also inherits model
142141
* attributes from the shared model for the request
143142
* @return this builder

0 commit comments

Comments
 (0)