Fragments

An example of a fragment

Back to overview

Local fragments

This illustrates a fragment from the same server.

Fragments work by defining an m:ref="" element on a page and exposing that page with a controller (in this example: SampleFragmentPartController).

That wrapper can then be referenced via an m:fragment="". The fragment's contents serve as a fallback in case the data cannot be loaded.

The following are all fragments loaded in dynamically:

Hello world

Hello world

This is a fallback that triggers if a-ref-that-does-not-exist does not exist.

Client

<!-- ref definition page (/fragments/sample-fragments.html) -->
<div m:ref="a-sample-ref">
    <p>Hello world</p>
</div>

<!-- root file (/sample/fragments.html) -->
<p>The following are all fragments loaded in dynamically: </p>

<m:fragment ref="a-sample-ref">
    <p>This is a fallback that triggers if a-sample-ref does not exist.</p>
</m:fragment>

<m:fragment ref="${myRefName}">
    <p>This is a fallback that triggers if
    <th:block th:text="${myRefName}"></th:block> does not exist.</p>
</m:fragment>

<m:fragment ref="${nonExistentRef}">
    <p>This is a fallback that triggers if
    <th:block th:text="${nonExistentRef}"></th:block> does not exist.</p>
</m:fragment>

Server

import io.getmedusa.medusa.core.annotation.UIEventPage;
import io.getmedusa.medusa.core.attributes.Attribute;

import java.util.List;

@UIEventPage(path = "/detail/sample/fragments", file = "/pages/sample/fragments.html")
public class FragmentController {

    public List<Attribute> setupAttributes(){
        return List.of(
                new Attribute("myRefName", "a-sample-ref"),
                new Attribute("nonExistentRef", "a-ref-that-does-not-exist"));
    }

}

/** different file **/
import io.getmedusa.medusa.core.annotation.UIEventPage;

@UIEventPage(path = "/fragment/sample", file = "/pages/fragments/sample-fragments.html")
public class SampleFragmentPartController { }

Remote fragments

This illustrates a forwarding decision from the serverside. Any action can return an Attribute that causes the user to forward.