|
17 | 17 | package org.springframework.web.reactive.result.view;
|
18 | 18 |
|
19 | 19 | import java.util.Collection;
|
20 |
| -import java.util.List; |
21 | 20 | import java.util.Map;
|
22 | 21 | import java.util.function.Consumer;
|
23 | 22 |
|
@@ -65,60 +64,122 @@ public interface FragmentsRendering {
|
65 | 64 | Flux<Fragment> fragments();
|
66 | 65 |
|
67 | 66 |
|
| 67 | + /** |
| 68 | + * Create a builder with one HTML fragment, also inheriting attributes from |
| 69 | + * the shared model for the request. |
| 70 | + * @param viewName the name of the view for the fragment |
| 71 | + * @return this builder |
| 72 | + * @since 6.2.1 |
| 73 | + */ |
| 74 | + static Builder fragment(String viewName) { |
| 75 | + return new DefaultFragmentsRenderingBuilder().fragment(viewName); |
| 76 | + } |
| 77 | + |
68 | 78 | /**
|
69 | 79 | * Create a builder with one HTML fragment.
|
70 | 80 | * @param viewName the view name for the fragment
|
71 | 81 | * @param model attributes for the fragment, in addition to attributes from the
|
72 | 82 | * shared model for the request
|
73 | 83 | * @return this builder
|
| 84 | + * @since 6.2.1 |
74 | 85 | */
|
75 |
| - static Builder with(String viewName, Map<String, Object> model) { |
76 |
| - return withCollection(List.of(Fragment.create(viewName, model))); |
| 86 | + static Builder fragment(String viewName, Map<String, Object> model) { |
| 87 | + return new DefaultFragmentsRenderingBuilder().fragment(viewName, model); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Create a builder with multiple HTML fragments. |
| 92 | + * @param fragments the fragments to add; each fragment also inherits |
| 93 | + * attributes from the shared model for the request |
| 94 | + * @return the created builder |
| 95 | + * @since 6.2.1 |
| 96 | + */ |
| 97 | + static Builder fragments(Collection<Fragment> fragments) { |
| 98 | + return new DefaultFragmentsRenderingBuilder().fragments(fragments); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Create a builder with a {@link Publisher} of fragments. |
| 103 | + * @param fragmentsPublisher the fragments to add; each fragment also |
| 104 | + * inherits model attributes from the shared model for the request |
| 105 | + * @return the created builder |
| 106 | + * @since 6.2.1 |
| 107 | + */ |
| 108 | + static <P extends Publisher<Fragment>> Builder fragmentsPublisher(P fragmentsPublisher) { |
| 109 | + return new DefaultFragmentsRenderingBuilder(fragmentsPublisher); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Variant of {@link #fragmentsPublisher(Publisher)} that allows using any |
| 114 | + * producer that can be resolved to {@link Publisher} via |
| 115 | + * {@link ReactiveAdapterRegistry}. |
| 116 | + * @since 6.2.1 |
| 117 | + */ |
| 118 | + static Builder fragmentsProducer(Object fragmentsProducer) { |
| 119 | + ReactiveAdapter adapter = ReactiveAdapterRegistry.getSharedInstance().getAdapter(fragmentsProducer.getClass()); |
| 120 | + Assert.isTrue(adapter != null, "Unknown producer " + fragmentsProducer.getClass()); |
| 121 | + Publisher<Fragment> publisher = adapter.toPublisher(fragmentsProducer); |
| 122 | + return fragmentsPublisher(publisher); |
77 | 123 | }
|
78 | 124 |
|
79 | 125 | /**
|
80 | 126 | * Create a builder with one HTML fragment, also inheriting attributes from
|
81 | 127 | * the shared model for the request.
|
82 | 128 | * @param viewName the name of the view for the fragment
|
83 | 129 | * @return this builder
|
| 130 | + * @deprecated in favor of {@link #fragment(String)} |
84 | 131 | */
|
| 132 | + @Deprecated(since = "6.2.1", forRemoval = true) |
85 | 133 | static Builder with(String viewName) {
|
86 |
| - return withCollection(List.of(Fragment.create(viewName))); |
| 134 | + return fragment(viewName); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Create a builder with one HTML fragment. |
| 139 | + * @param viewName the view name for the fragment |
| 140 | + * @param model attributes for the fragment, in addition to attributes from the |
| 141 | + * shared model for the request |
| 142 | + * @return this builder |
| 143 | + * @deprecated in favor of {@link #fragment(String, Map)} |
| 144 | + */ |
| 145 | + @Deprecated(since = "6.2.1", forRemoval = true) |
| 146 | + static Builder with(String viewName, Map<String, Object> model) { |
| 147 | + return fragment(viewName, model); |
87 | 148 | }
|
88 | 149 |
|
89 | 150 | /**
|
90 | 151 | * Create a builder with multiple HTML fragments.
|
91 | 152 | * @param fragments the fragments to add; each fragment also inherits
|
92 | 153 | * attributes from the shared model for the request
|
93 | 154 | * @return the created builder
|
| 155 | + * @deprecated in favor of {@link #fragments(Collection)} |
94 | 156 | */
|
| 157 | + @Deprecated(since = "6.2.1", forRemoval = true) |
95 | 158 | static Builder withCollection(Collection<Fragment> fragments) {
|
96 |
| - return new DefaultFragmentsRenderingBuilder(fragments); |
| 159 | + return fragments(fragments); |
97 | 160 | }
|
98 | 161 |
|
99 | 162 | /**
|
100 | 163 | * Create a builder with a {@link Publisher} of fragments.
|
101 | 164 | * @param fragmentsPublisher the fragments to add; each fragment also
|
102 | 165 | * inherits model attributes from the shared model for the request
|
103 | 166 | * @return the created builder
|
| 167 | + * @deprecated in favor of {@link #fragmentsPublisher(Publisher)} |
104 | 168 | */
|
| 169 | + @Deprecated(since = "6.2.1", forRemoval = true) |
105 | 170 | static <P extends Publisher<Fragment>> Builder withPublisher(P fragmentsPublisher) {
|
106 |
| - return new DefaultFragmentsRenderingBuilder(fragmentsPublisher); |
| 171 | + return fragmentsPublisher(fragmentsPublisher); |
107 | 172 | }
|
108 | 173 |
|
109 | 174 | /**
|
110 |
| - * Variant of {@link #withPublisher(Publisher)} that allows using any |
| 175 | + * Variant of {@link #fragmentsPublisher(Publisher)} that allows using any |
111 | 176 | * producer that can be resolved to {@link Publisher} via
|
112 | 177 | * {@link ReactiveAdapterRegistry}.
|
| 178 | + * @deprecated in favor of {@link #fragmentsProducer(Object)} |
113 | 179 | */
|
| 180 | + @Deprecated(since = "6.2.1", forRemoval = true) |
114 | 181 | static Builder withProducer(Object fragmentsProducer) {
|
115 |
| - return new DefaultFragmentsRenderingBuilder(adaptProducer(fragmentsProducer)); |
116 |
| - } |
117 |
| - |
118 |
| - private static Publisher<Fragment> adaptProducer(Object producer) { |
119 |
| - ReactiveAdapter adapter = ReactiveAdapterRegistry.getSharedInstance().getAdapter(producer.getClass()); |
120 |
| - Assert.isTrue(adapter != null, "Unknown producer " + producer.getClass()); |
121 |
| - return adapter.toPublisher(producer); |
| 182 | + return fragmentsProducer(fragmentsProducer); |
122 | 183 | }
|
123 | 184 |
|
124 | 185 |
|
@@ -169,11 +230,21 @@ interface Builder {
|
169 | 230 |
|
170 | 231 | /**
|
171 | 232 | * Add an HTML fragment.
|
172 |
| - * @param fragment the fragment to add |
| 233 | + * @param fragment the fragment to add; the fragment also inherits |
| 234 | + * attributes from the shared model for the request |
173 | 235 | * @return this builder
|
174 | 236 | */
|
175 | 237 | Builder fragment(Fragment fragment);
|
176 | 238 |
|
| 239 | + /** |
| 240 | + * Add HTML fragments. |
| 241 | + * @param fragments the fragments to add; each fragment also inherits |
| 242 | + * attributes from the shared model for the request |
| 243 | + * @return this builder |
| 244 | + * @since 6.2.1 |
| 245 | + */ |
| 246 | + Builder fragments(Collection<Fragment> fragments); |
| 247 | + |
177 | 248 | /**
|
178 | 249 | * Build the {@link FragmentsRendering} instance.
|
179 | 250 | */
|
|
0 commit comments