Thursday, 2 August 2018

Basic Sling Model Exporter

/*
Open a page which is having component /apps/project/components/page/page
/content/project/en/jcr:content.model.json
Don't miss jcr:content in the path. Add jcr:content.model.json
*/
package com.org.project.models;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Required;
import org.apache.sling.models.annotations.Source;
import org.apache.sling.models.annotations.injectorspecific.Self;
import com.day.cq.wcm.api.Page;
@Model(adaptables = Resource.class, resourceType = "/apps/project/components/page/page")
@Exporter(name = "jackson", extensions = "json")
public class PageModelExporter {
@Self
private Resource resource;
@Inject @Named("jcr:title") @Required
private String title;
@PostConstruct
private void init() {
}
public String getTitle() {
return title;
}
}

Basic Sling Model Example

/*
<div data-sly-use.object="com.org.project.models.PageModel"/>
${object.title}
</div>
*/
package com.org.project.models;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Required;
import org.apache.sling.models.annotations.injectorspecific.Self;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
@Model(
adaptables = Resource.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class PageModel {
@Self
private Resource resource;
@Inject @Named("jcr:title") @Required
private String title;
@PostConstruct
private void init() {
}
public String getTitle() {
return title;
}
}

Basic Sling Model Exporter

/* Open a page which is having component /apps/project/components/page/page /content/project/en/jcr:content.model.json Don't miss...