Org Skills

Sling Models for AEM components — full samples

Supporting material for aem-component-development. Agents load it on demand; it ships inside the skill folder.

RawSource

Verified: 2026-09 against https://experienceleague.adobe.com/en/docs/experience-manager-core-components/using/developing/delegation-pattern and https://sling.apache.org/documentation/bundles/models.html

Open this for a complete component model, the Core Components delegation pattern, or the shape of .model.json output.

A component model

@Model(adaptables = SlingHttpServletRequest.class,
       adapters = {Hero.class, ComponentExporter.class},
       resourceType = HeroImpl.RESOURCE_TYPE,
       defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME,
          extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class HeroImpl implements Hero {

    static final String RESOURCE_TYPE = "acme/components/hero";

    @ValueMapValue private String title;
    @ChildResource private List<Resource> links;     // composite multifield
    @ScriptVariable private Page currentPage;
    @ScriptVariable private Style currentStyle;      // policy values
    @OSGiService private LinkResolver linkResolver;
    @SlingObject private Resource resource;

    private List<HeroLink> heroLinks;

    @PostConstruct
    protected void init() {
        heroLinks = links == null ? List.of()
            : links.stream().map(r -> new HeroLink(r, linkResolver)).toList();
    }

    @Override public String getTitle() { return title; }
    @Override public List<HeroLink> getLinks() { return heroLinks; }
    @Override @JsonIgnore public boolean isEmpty() { return StringUtils.isBlank(title); }
    @Override public String getExportedType() { return RESOURCE_TYPE; }
}
  • Adapt from SlingHttpServletRequest for component models: that is what gives you currentPage, currentStyle, WCMMode and i18n. Resource-adaptable models are for helpers and background code.
  • Use injector-specific annotations. Bare @Inject walks every injector in turn, which is both slower and ambiguous about where the value came from.
  • DefaultInjectionStrategy.OPTIONAL at class level, then handle nulls. With the default (required) strategy, a freshly dropped component fails to adapt and renders nothing — this is the single most common "why is my component blank" cause.
  • Keep the interface in an exported package and *Impl in an internal one. HTL, exporters and tests depend on the interface.
  • Models run on every render and every .model.json request. No queries, no HTTP, no side effects.

Delegation: extending a Core Component model

@Model(adaptables = SlingHttpServletRequest.class, adapters = Teaser.class,
       resourceType = "acme/components/teaser",
       defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class AcmeTeaser implements Teaser {

    @Self @Via(type = ResourceSuperType.class)
    @Delegate(excludes = Excluded.class)     // Lombok; or hand-write the pass-throughs
    private Teaser delegate;

    @ValueMapValue private String badge;

    public String getBadge() { return badge; }

    @Override public String getTitle() { return StringUtils.upperCase(delegate.getTitle()); }

    private interface Excluded { String getTitle(); }   // methods you override yourself
}
  • @Via(type = ResourceSuperType.class) adapts the resource as its super type, so you get the Core implementation without extending it.
  • Never subclass or copy a Core *Impl. Those classes are internal and change between Core Components releases; the interfaces are the contract.
  • Add new getters to your own interface (AcmeTeaser extends Teaser) so HTL and the exporter can see them.

Exporter output

GET /content/acme/en/home/jcr:content/root/container.model.json on a container returns:

{
  ":type": "acme/components/container",
  ":itemsOrder": ["hero", "teaser"],
  ":items": {
    "hero":   { ":type": "acme/components/hero", "title": "Spring release", "links": [] },
    "teaser": { ":type": "acme/components/teaser", "badge": "New", "title": "READ MORE" }
  }
}
  • :type is getExportedType(); :items and :itemsOrder come from ComponentExporter containers (ContainerExporter).
  • Shape the JSON with Jackson: @JsonProperty("linkUrl"), @JsonIgnore on internals, @JsonInclude(JsonInclude.Include.NON_EMPTY) on the class to keep responses small.
  • Never serialise Resource, Page, ValueMap or a ResourceResolver: the output leaks repository paths and can recurse. Map to small DTOs.
  • .model.json is a public endpoint once the dispatcher allows the model selector — treat what it returns as published content, and check it in review.

Universal Editor readiness

A model that already exposes a clean exported shape is most of the work for the Universal Editor:

  • Keep property names stable and meaningful; the editor writes back to the same property names the model reads.
  • Core Components emit data-aue-* instrumentation themselves. For a custom component, either emit the attributes in HTL or expose the values the front end needs to emit them — see aem-universal-editor.
  • Anything the editor must be able to change has to be a real JCR property on the component's resource, not a value computed from the page or derived in @PostConstruct.

Testing

@ExtendWith(AemContextExtension.class)
class HeroImplTest {
    private final AemContext ctx = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK);

    @BeforeEach void setUp() {
        ctx.addModelsForClasses(HeroImpl.class);
        ctx.load().json("/hero.json", "/content/acme");
    }

    @Test void readsTitle() {
        ctx.currentResource("/content/acme/hero");
        Hero hero = ctx.request().adaptTo(Hero.class);
        assertEquals("Spring release", hero.getTitle());
    }
}

Full patterns, fixtures and what to cover live in aem-testing.