Sling Models in AEM

Sling models is the most used in AEM to develop the components. Sling models is something, which works on Sling framework. Basically, for every field for Components Dialog we can have Sling model. Means what ever fields we have in dialog we usually write sling Sling models for them.

In sling Class we inject the Fields using the @Inject Annotation Or we can also use @ValueMapValue annotation as well. You can get more information regarding Sling annotations here.

We also have @postConstruct annotation also which is get executed after all the injection in the class.

For example, we have a Product Component with fields Name and email



The Sling model class for that will be-

package com.aemtraining.core.models;

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

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

import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;

@Model(adaptables = Resource.class)

public class Product {

private final Logger logger = LoggerFactory.getLogger(getClass());

@ValueMapValue
private String name;

@ValueMapValue
private String email;

@PostConstruct

protected void init() {

logger.info(“In init of State2 Model”);}

public String getName() {  return name;}

public String getEmail() {  return email;

}}

We are injecting the Fields/variable using the @ValueMapValue annotations and also used the @postConstruct annotation for printing the logs.

so the xml for that will be-


<?xml version=“1.0” encoding=“UTF-8”?>
<jcr:root xmlns:sling=“http://sling.apache.org/jcr/sling/1.0” xmlns:cq=“http://www.day.com/jcr/cq/1.0” xmlns:jcr=“http://www.jcp.org/jcr/1.0” xmlns:nt=“http://www.jcp.org/jcr/nt/1.0”
jcr:primaryType=“nt:unstructured”
jcr:title=“Product”
sling:resourceType=“cq/gui/components/authoring/dialog”>

<content
jcr:primaryType=“nt:unstructured”
sling:resourceType=“granite/ui/components/coral/foundation/fixedcolumns”>

<items jcr:primaryType=“nt:unstructured”>
<column
jcr:primaryType=“nt:unstructured”
sling:resourceType=“granite/ui/components/coral/foundation/container”>

<items jcr:primaryType=“nt:unstructured”>
<field
jcr:primaryType=“nt:unstructured”
sling:resourceType=“granite/ui/components/coral/foundation/container”
name=“./products”>

<items jcr:primaryType=“nt:unstructured”>
<column
jcr:primaryType=“nt:unstructured”
sling:resourceType=“granite/ui/components/coral/foundation/container”>

<items jcr:primaryType=“nt:unstructured”>
<name
jcr:primaryType=“nt:unstructured”
sling:resourceType=“granite/ui/components/coral/foundation/form/textfield”
fieldDescription=“Name of Product”
fieldLabel=“Product Name”
name=“./name”/>

<email
jcr:primaryType=“nt:unstructured”
sling:resourceType=“granite/ui/components/coral/foundation/form/textfield”
fieldLabel=“Email”
name=“./email”/>

</items>
</column>
</items>
</field>
</items>
</column>
</items>
</content>
</jcr:root>

And HTL file is here-




<div>
<b>Product Details</b>
<br><br>
<div data-sly-use.product=“com.aemtraining.core.models.Product” data-sly-unwrap>
<div data-sly-test=“${!product || wcmmode.edit}”>
Add state details using component dialog
</div>
<div>Name: ${product.name}</b></div>
<div>Email: ${product.email}</div>
</div>
</div>
</div>




Build and Deploy you code.By using the command below.

mvn clean install -PautoInstallPackage


If you really find this post useful, you can help me to keep this alive by buying me a cup of Coffee.

Stay Safe & Healthy              Happy Learning Coding