Update method names in FragmentsRendering

Closes gh-33974
This commit is contained in:
rstoyanchev
2024-11-27 11:21:19 +00:00
parent 186f909c96
commit 81ea35c726
11 changed files with 173 additions and 55 deletions

View File

@@ -92,7 +92,7 @@ public class ModelAndViewMethodReturnValueHandler implements HandlerMethodReturn
}
if (returnValue instanceof Collection<?> mavs) {
returnValue = FragmentsRendering.with((Collection<ModelAndView>) mavs).build();
returnValue = FragmentsRendering.fragments((Collection<ModelAndView>) mavs).build();
}
if (returnValue instanceof FragmentsRendering rendering) {

View File

@@ -393,7 +393,7 @@ public class ResponseBodyEmitterReturnValueHandler implements HandlerMethodRetur
FragmentHttpServletResponse fragmentResponse =
new FragmentHttpServletResponse(this.response, this.charset);
FragmentsRendering render = FragmentsRendering.with(List.of(modelAndView)).build();
FragmentsRendering render = FragmentsRendering.fragments(List.of(modelAndView)).build();
render.resolveNestedViews(this::resolveViewName, this.locale);
render.render(modelAndView.getModel(), this.request, fragmentResponse);

View File

@@ -54,37 +54,77 @@ public interface FragmentsRendering extends SmartView {
HttpHeaders headers();
/**
* Create a builder with one HTML fragment, also inheriting attributes from
* the shared model for the request.
* @param viewName the name of the view for the fragment
* @return the created builder
* @since 6.2.1
*/
static Builder fragment(String viewName) {
return new DefaultFragmentsRenderingBuilder().fragment(viewName);
}
/**
* Create a builder with one HTML fragment.
* @param viewName the view name for the fragment
* @param model attributes for the fragment, in addition to attributes from the
* shared model for the request
* @return the created builder
* @since 6.2.1
*/
static Builder with(String viewName, Map<String, Object> model) {
static Builder fragment(String viewName, Map<String, Object> model) {
return new DefaultFragmentsRenderingBuilder().fragment(viewName, model);
}
/**
* Create a builder with one HTML fragment, also inheriting attributes from
* the shared model for the request.
* @param viewName the name of the view for the fragment
* @return the created builder
*/
static Builder with(String viewName) {
return new DefaultFragmentsRenderingBuilder().fragment(viewName);
}
/**
* Create a builder with multiple HTML fragments.
* @param fragments the fragments to add; each fragment also inherits
* attributes from the shared model for the request
* @return the created builder
* @since 6.2.1
*/
static Builder with(Collection<ModelAndView> fragments) {
static Builder fragments(Collection<ModelAndView> fragments) {
return new DefaultFragmentsRenderingBuilder().fragments(fragments);
}
/**
* Create a builder with one HTML fragment, also inheriting attributes from
* the shared model for the request.
* @param viewName the name of the view for the fragment
* @return the created builder
* @deprecated in favor of {@link #fragment(String)}
*/
@Deprecated(since = "6.2.1", forRemoval = true)
static Builder with(String viewName) {
return fragment(viewName);
}
/**
* Create a builder with one HTML fragment.
* @param viewName the view name for the fragment
* @param model attributes for the fragment, in addition to attributes from the
* shared model for the request
* @return the created builder
* @deprecated in favor of {@link #fragment(String, Map)}
*/
@Deprecated(since = "6.2.1", forRemoval = true)
static Builder with(String viewName, Map<String, Object> model) {
return fragment(viewName, model);
}
/**
* Create a builder with multiple HTML fragments.
* @param fragments the fragments to add; each fragment also inherits
* attributes from the shared model for the request
* @return the created builder
* @deprecated in favor of {@link #fragments(Collection)}
*/
@Deprecated(since = "6.2.1", forRemoval = true)
static Builder with(Collection<ModelAndView> fragments) {
return fragments(fragments);
}
/**
* Defines a builder for {@link FragmentsRendering}.

View File

@@ -91,7 +91,7 @@ class ModelAndViewMethodReturnValueHandlerTests {
@Test
void handleFragmentsRendering() throws Exception {
FragmentsRendering rendering = FragmentsRendering.with("viewName").build();
FragmentsRendering rendering = FragmentsRendering.fragment("viewName").build();
handler.handleReturnValue(rendering, returnParamModelAndView, mavContainer, webRequest);
assertThat(mavContainer.getView()).isInstanceOf(SmartView.class);

View File

@@ -56,7 +56,8 @@ public class DefaultFragmentsRenderingTests {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
FragmentsRendering view = FragmentsRendering.with("fragment1", Map.of("foo", "Foo"))
FragmentsRendering view = FragmentsRendering
.fragment("fragment1", Map.of("foo", "Foo"))
.fragment("fragment2", Map.of("bar", "Bar"))
.header("headerName", "headerValue")
.build();