How to make Sling Interface in AEM

Here I am going to tell you that we can also use the Sling interface instead of the Sling model.

We use sling model in case where we have to do manipulation over data. But where we want to show only values we can use sling interface.

Sling Interface is shown below. In Sling Interface we have to inject the getter of our node property.

Interface

package com.lhotse.aem.interview.core.models;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.models.annotations.DefaultInjectionStrategy;

import org.apache.sling.models.annotations.Model;

import javax.inject.Inject;

@Model(adaptables = Resource.class,defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)

public interface SlingInterface {

   @Inject

   public String getSubTitle();

   @Inject

   public String getTitle();

}

In Sling Interface, we make getter of properties and inject in the same way as we do in Sling Model.

We can call Sling Interface in sightly using data-sly-use in sling-Interface.html where sling-interface is the name of the component.

I.e

sling-interface.html

<sly data-sly-use.interface=”com.lhotse.aem.interview.core.models.SlingInterface”></sly>

${interface.title}<br>

${interface.subTitle}

And when you will drop this component onto the page then you will get the values.

Authoring properties.

On View as Published mode.