Service using Content Fragement
How to fetch all details from content fragment using services like OSGI services using sling model
How to fetch all details from content fragment using services like OSGI services using sling model
package com.myorg.core.models;
import java.util.UUID;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
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.Optional;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import com.adobe.cq.dam.cfm.ContentFragment;
import static java.util.Objects.nonNull;
@Model(adaptables = { SlingHttpServletRequest.class, Resource.class })
public class ContentFragModel {
@ScriptVariable
private Resource resource;
@ValueMapValue(name = "fragmentPath")
@7392697
private String fragmentPath;
private String title;
private String pageUrl;
private String line1;
private String line2;
@PostConstruct
protected void init() {
if(fragmentPath != null) {
Resource contentRes = resource.getResourceResolver().getResource(fragmentPath);
if (nonNull(contentRes)) {
ContentFragment cf = contentRes.adaptTo(ContentFragment.class);
if (nonNull(cf)) {
title = cf.hasElement("name") ? cf.getElement("name").getContent() : StringUtils.EMPTY;
pageUrl = cf.hasElement("page-url") ? cf.getElement("page-url").getContent() : StringUtils.EMPTY;
line1 = cf.hasElement("line1") ? cf.getElement("line1").getContent() : StringUtils.EMPTY;
line2 = cf.hasElement("line2") ? cf.getElement("line2").getContent() : StringUtils.EMPTY;
}
}
}
}
/**
* @2007960 the title
*/
public String getTitle() {
return title;
}
/**
* @2007960 the pageUrl
*/
public String getPageUrl() {
return pageUrl;
}
/**
* @2007960 the line1
*/
public String getLine1() {
return line1;
}
/**
* @2007960 the line2
*/
public String getLine2() {
return line2;
}
}
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.