AEM Sling Model
Sling Model is a pojo class use to map resource or request objects. They also allows us to map resource properties and inject OSGI services. Using sling models we can export JSON.
A Sling Model is implemented as an OSGi bundle. We can convert pojo class to Sling model with the help of @Model annotation declare on top of the class.
Let’s create a sling model component to Inject current resource property and an OSGI service with the help of @Inject and @OSGiService respectively.
Follow below step to create Sling model class:
As part of this blog we are going to use same code for R6 annotation OSGI service we have written as part of this blog.
1. Create any component having title property as shown below:
2. Create a PracticeModel.java class and mention @Model annotation on top of the class and declare adaptables property value as Resource.class as mentioned in below code.
Map resource property using @Inject annotation. e.g. we are injecting name authorable property created as part of above component.
Use @OSGiService annotation to consume or inject an OSGI service. Here, we are consuming OSGI service created as part of OSGI Service tutorial.
package com.javadoubts.core.models;
import com.javadoubts.core.services.PracticeOCDService;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Optional;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
@Model(adaptables = Resource.class)
public class PracticeModel {
@Default(values="Test")
@Inject
@Optional
@Named("title")
protected String heading;
@OSGiService
PracticeOCDService practiceOCDService;
private String name;
@PostConstruct
protected void init() {
name = practiceOCDService.getName();
}
public String getName() {
return name;
}
public String getHeading() {
return heading;
}
}
@Default annotation will consider default value if no value authored.
@Required annotation make sure resource having property.
@Optional annotation is to tell model field is optional.
@Named(“property_name”) annotation in case field name didn’t get match with property name.
@PostConstruct method gets call once all fields injections are done. It allows to perform operations on top of the injected fields and call require methods to perform required operations.
@PostConstruct
method can false
which will fail model creation with logging any exception at debug level.
3. Sling model can be consume with the help of data-sly-use attribute as shown below.
<sly data-sly-use.practice="com.javadoubts.core.models.PracticeModel"/><h2>Sling Model</h2>
<p>Message: ${practice.title}</p>
<p>Name From Service: ${practice.name}</p>
4. Below is the output:
@Via
In some cases, a different object should be used as the adaptable instead of the original adaptable. This can be done using the @Via annotation.
package com.javadoubts.core.models;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Via;
import javax.inject.Inject;
@Model(adaptables= SlingHttpServletRequest.class)
public interface ViaModelExample {
// This wil return request.getResource().getValueMap().get("propertyName", String.class)
@Inject
@Via("resource")
String getPropertyName();
}
@Source
In case of ambiguity to inject value from one of the injector. Use @Source annotation to inject from required resource:
package com.javadoubts.core.models;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Source;
import javax.inject.Inject;
@Model(adaptables= SlingHttpServletRequest.class)
public interface ViaModelExample {
// Retrieve the resource from bindings not from request
@Inject @Source("script-bindings")
Resource getResource();
}
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.