In this article we will learn about how we can create a sling model in AEM and then use it in slightly in AEM.
Create Component on Instance
- Go to your AEM instance and then create a component with cq:dailog and add your required properties.
- rename jsp file to .html
- In .html file write the path of your sling model and print the properties with object of sling model
Create Sling Model
sling Model Interface
package sa.com.stc.core.models;
import com.adobe.cq.export.json.ComponentExporter;
public interface AAA extends ComponentExporter {
String getTitle();
String getDescription();
}
package path of interface is used load the sling model in component
Sling Model Implementation class
package sa.com.stc.core.models.Impl;
import javax.inject.Inject;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import sa.com.stc.core.services.CxpOsgiConfig;
import sa.com.stc.core.models.AAA;
@Model(adaptables = SlingHttpServletRequest.class,
adapters = AAA.class,
defaultInjectionStrategy=DefaultInjectionStrategy.OPTIONAL
)
public class AAAImpl implements AAA{
@Inject
@OSGiService
CxpOsgiConfig cxpOsgiConfig;
@ValueMapValue
@Default(values ="tit")
String title;
@ValueMapValue
@Default(values = "desp")
String description;
@Override
public String getTitle()
{
return title +cxpOsgiConfig.getBaseUrl();
}
@Override
public String getDescription()
{
return description + cxpOsgiConfig.getPort();
}
@Override
public String getExportedType() {
return "Nothing";
}
}
Annotation
@Modeladpatables: explain how this model will behave
adpater : write the name of your interface (in case your are not using interface you can skip it)
@ValueMapValue
Used specifically to map content properties to java fields in the sling model
@Reuired : help to make the value reqauired
@ Default:to add default value
@OSGIService is used to call the osgi service in sling model
@Inject also used of same als ValueMapValue you can use one of them.
Build and Deploy this Model to AEM
goto core folder of your project and run following commandmvn clean install -PautoInstallBundle
Thanks
0 comments: