-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates to WebMVC fragment rendering API
See gh-33162
- Loading branch information
1 parent
6ee8786
commit 14c1faa
Showing
6 changed files
with
230 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
.../src/main/java/org/springframework/web/servlet/view/DefaultFragmentsRenderingBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
116 changes: 116 additions & 0 deletions
116
spring-webmvc/src/main/java/org/springframework/web/servlet/view/FragmentsRendering.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters