0

I just start using Sling models, and I have an issue to retrieve a child node property in the parent model. Here is my JCR structure

the image node is a from the foundation components. and my aim is to get the "filerefernce" property of the image component in the Topbanner node then in its sightly script. here is my topbanner node model :

@Model(adaptables=Resource.class)
public class TopBanner {



 @Self @Via("resource")
 private Resource bannerBackGroundImage;

 private String bannerBgImagePath;

 // @Inject 
 // private String bannerTitle;

 // @Inject 
 // private String bannerDescription;
 // 
 // @Inject 
 // private String bannerButtonText;
 // 
 // @Inject 
 // private String bannerButtonLink;

  @SlingObject
  private ResourceResolver resourceResolver;

  @PostConstruct
  public void init() {
    TopBanner.LOG.info("we are here");

    try {
bannerBackGroundImage=resourceResolver.getResource("/apps/ads/components/structure/TopBanner2/Image");
        this.bannerBgImagePath=bannerBackGroundImage.adaptTo(ValueMap.class).get("fileReference",String.class);
    } catch(SlingException e) {
        TopBanner.LOG.info("Error message  **** " + e.getMessage());
    }   

}
// getters omitted 

the error I am getting is Identifier Mypackage.models.TopBanner cannot be correctly instantiated by the Use API

k.h.t
  • 37
  • 5
  • did any of the below answers work out for you? you might want to accept an answer for the benefit of the community, just might help others stumbling upon a similar issue – SubSul Sep 21 '18 at 05:59

2 Answers2

0

If your target is to get 'fileReference' try this:

@Self
private SlingHttpServletRequest request;

@ValueMapValue(name = DownloadResource.PN_REFERENCE, injectionStrategy = InjectionStrategy.OPTIONAL)
private String fileReference;

then to get our asset use following:

if (StringUtils.isNotEmpty(fileReference)) {
        // the image is coming from DAM
        final Resource assetResource = request.getResourceResolver().getResource(fileReference);
        if (assetResource != null) {
            Asset asset = assetResource.adaptTo(Asset.class);
            //Work with your asset there.
        }
    }

also add to your class annotation:

@Model(adaptables = { SlingHttpServletRequest.class })
Prosis
  • 36
  • 3
0

Using @ChildResource annotation

  @ChildResource
  @Named("image") //Child node name
  private Resource childResource;

  private String imagePath;

  public String getImagePath() {
    return imagePath;
  }

  @PostConstruct
  public void init() {
    imagePath = childResource.getValueMap().get("fileReference", String.class);
  }

Retrieve the imagePath in Sightly/HTL markup using

<div data-sly-use.model="package.name.TopBanner">
  <img src="${model.imagePath}"/>
</div>

Another way according to Sling documentation docs is using @Via annotation, since Sling model API 1.3.4.

Example from the documentation,

@Model(adaptables=Resource.class)
public interface MyModel {

    // will return resource.getChild("jcr:content").getValueMap().get("propertyName", String.class)
    @Inject @Via(value = "jcr:content", type = ChildResource.class)
    String getPropertyName();

}
SubSul
  • 2,523
  • 1
  • 17
  • 27