6 changed files with 230 additions and 27 deletions
@ -0,0 +1,64 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2024 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. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.web.servlet.view; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Collection; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
import org.springframework.web.servlet.ModelAndView; |
||||||
|
|
||||||
|
/** |
||||||
|
* Default {@link FragmentsRendering.Builder} implementation that collects the |
||||||
|
* fragments and creates a {@link DefaultFragmentsRendering}. |
||||||
|
* |
||||||
|
* @author Rossen Stoyanchev |
||||||
|
* @since 6.2 |
||||||
|
*/ |
||||||
|
final class DefaultFragmentsRenderingBuilder implements FragmentsRendering.Builder { |
||||||
|
|
||||||
|
private final Collection<ModelAndView> fragments = new ArrayList<>(); |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public DefaultFragmentsRenderingBuilder fragment(String viewName, Map<String, Object> model) { |
||||||
|
return fragment(new ModelAndView(viewName, model)); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public DefaultFragmentsRenderingBuilder fragment(String viewName) { |
||||||
|
return fragment(new ModelAndView(viewName)); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public DefaultFragmentsRenderingBuilder fragment(ModelAndView fragment) { |
||||||
|
this.fragments.add(fragment); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public DefaultFragmentsRenderingBuilder fragments(Collection<ModelAndView> fragments) { |
||||||
|
this.fragments.addAll(fragments); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public FragmentsRendering build() { |
||||||
|
return new DefaultFragmentsRendering(this.fragments); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,116 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2024 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. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.web.servlet.view; |
||||||
|
|
||||||
|
import java.util.Collection; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
import org.springframework.web.servlet.ModelAndView; |
||||||
|
import org.springframework.web.servlet.SmartView; |
||||||
|
|
||||||
|
/** |
||||||
|
* Public API for HTML rendering a collection fragments each with its own view |
||||||
|
* and model. For use with view technologies such as |
||||||
|
* <a href="https://htmx.org/">htmx</a> where multiple page fragments may be |
||||||
|
* rendered in a single response. Supported as a return value from a Spring MVC |
||||||
|
* controller method. |
||||||
|
* |
||||||
|
* @author Rossen Stoyanchev |
||||||
|
* @since 6.2 |
||||||
|
*/ |
||||||
|
public interface FragmentsRendering extends SmartView { |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Create a builder for {@link FragmentsRendering}, adding a fragment with |
||||||
|
* the given view name and model. |
||||||
|
* @param viewName the name of the view for the fragment |
||||||
|
* @param model attributes for the fragment in addition to model |
||||||
|
* attributes inherited from the shared model for the request |
||||||
|
* @return the created builder |
||||||
|
*/ |
||||||
|
static Builder with(String viewName, Map<String, Object> model) { |
||||||
|
return new DefaultFragmentsRenderingBuilder().fragment(viewName, model); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Variant of {@link #with(String, Map)} with a view name only, but also |
||||||
|
* inheriting model 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); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Variant of {@link #with(String, Map)} with a collection of fragments. |
||||||
|
* @param fragments the fragments to add; each fragment also inherits model |
||||||
|
* attributes from the shared model for the request |
||||||
|
* @return the created builder |
||||||
|
*/ |
||||||
|
static Builder with(Collection<ModelAndView> fragments) { |
||||||
|
return new DefaultFragmentsRenderingBuilder().fragments(fragments); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Defines a builder for {@link FragmentsRendering}. |
||||||
|
*/ |
||||||
|
interface Builder { |
||||||
|
|
||||||
|
/** |
||||||
|
* Add a fragment with a view name and a model. |
||||||
|
* @param viewName the name of the view for the fragment |
||||||
|
* @param model attributes for the fragment in addition to model |
||||||
|
* attributes inherited from the shared model for the request |
||||||
|
* @return this builder |
||||||
|
*/ |
||||||
|
Builder fragment(String viewName, Map<String, Object> model); |
||||||
|
|
||||||
|
/** |
||||||
|
* Add a fragment with a view name only, inheriting model attributes from |
||||||
|
* the model for the request. |
||||||
|
* @param viewName the name of the view for the fragment |
||||||
|
* @return this builder |
||||||
|
*/ |
||||||
|
Builder fragment(String viewName); |
||||||
|
|
||||||
|
/** |
||||||
|
* Add a fragment. |
||||||
|
* @param fragment the fragment to add; the fragment also inherits model |
||||||
|
* attributes from the shared model for the request |
||||||
|
* @return this builder |
||||||
|
*/ |
||||||
|
Builder fragment(ModelAndView fragment); |
||||||
|
|
||||||
|
/** |
||||||
|
* Add a collection of fragments. |
||||||
|
* @param fragments the fragments to add; each fragment also inherits model |
||||||
|
* attributes from the shared model for the request |
||||||
|
* @return this builder |
||||||
|
*/ |
||||||
|
Builder fragments(Collection<ModelAndView> fragments); |
||||||
|
|
||||||
|
/** |
||||||
|
* Build a {@link FragmentsRendering} instance. |
||||||
|
*/ |
||||||
|
FragmentsRendering build(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue