Monday, 24 July 2017

Creating Adobe Experience Manager 6.3 Sling Model Components

You can work with Sling Models when developing with Adobe Experience Manager (AEM) 6.3. That is, when developing an Experience Manager project, you can define a model object (a Java object) and map that object to Sling resources. For more information, see Sling Models.

A Sling Model is implemented as an OSGi bundle. A Java class located in the OSGi bundle is annotated with @Model and the adaptable class (for example, @Model(adaptables = Resource.class). The data members (Fields) use @Inject annotations. These data members map to node properties.

Consider the following Java class named UserInfo.

package SlingModel63.core;
  
import javax.inject.Inject;
  
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
   
@Model(adaptables = Resource.class)
public class UserInfo {
    @Inject
    private String firstName;
    @Inject
    private String lastName;
    @Inject
    private String technology;
       
    public String getFirstName() {
        return firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public String getTechnology() {
        return technology;
    }
   
}

As you can see in the code example, the @Model annotation is used. Likewise, each data member in the UserInfo class is annotated using the @Inject annotation. This Java class is mapped to a Sling resource, like the one shown in the following illustration.



Notice that the class members in the UserInfo class map to the String properties that belong to the /content/testsling/slingmodel node. This article walks you through creating a Sling Servlet that uses a Sling Model to map to this resource.

The following illustration shows the output of the Sling Servlet that uses Sling Models. Notice that the values in the node properties are displayed.



Join the Experience League

To become an Experience Business, you need more than just great tools and online help. You need a partner. Experience League is a new enablement program with guided learning to help you get the most out of Adobe Experience Cloud. With training materials, one-to-one expert support, and a thriving community of fellow professionals, Experience League is a comprehensive program designed to help you become your best.

Join the Adobe Experience League by clicking this banner.




I (Scott Macdonald) am a Senior Experience League Community Manager at Adobe Systems with 20 years in the high tech industry. I am also a programmer with knowledge in Java, JavaScript, C#,C++, HTML, XML and ActionScript. If  you would like to see more AEM or other end to end articles like this, then leave a comment and let me know what content you would like to see.

Linked In: http://www.linkedin.com/in/scottmacdonald2010

YouTube: Subscribe to the AEM Community Channel.