Aem 在父节点吊索模型中获得基础构件属性
我刚开始使用Sling模型,在父模型中检索子节点属性时遇到了一个问题。Aem 在父节点吊索模型中获得基础构件属性,aem,sling,jcr,sling-models,Aem,Sling,Jcr,Sling Models,我刚开始使用Sling模型,在父模型中检索子节点属性时遇到了一个问题。 图像节点是来自基础组件的A。 我的目标是在Topbanner节点中获得图像组件的“FileReference”属性,然后在它的脚本中。 以下是我的topbanner节点模型: @Model(adaptables=Resource.class) public class TopBanner { @Self @Via("resource") private Resource bannerBackGroundImage
图像节点是来自基础组件的A。 我的目标是在Topbanner节点中获得图像组件的“FileReference”属性,然后在它的脚本中。 以下是我的topbanner节点模型:
@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
我得到的错误是
标识符Mypackage.models.TopBanner无法通过使用API正确实例化如果目标是获取“fileReference”,请尝试以下操作:
@Self
private SlingHttpServletRequest request;
@ValueMapValue(name = DownloadResource.PN_REFERENCE, injectionStrategy = InjectionStrategy.OPTIONAL)
private String fileReference;
然后,要获得我们的资产使用,请执行以下操作:
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.
}
}
还要将以下内容添加到类注释中:
@Model(adaptables = { SlingHttpServletRequest.class })
使用
@ChildResource
注释
@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);
}
使用检索Sightly/HTL标记中的imagePath
<div data-sly-use.model="package.name.TopBanner">
<img src="${model.imagePath}"/>
</div>
下面的答案对你有用吗?为了社区的利益,你可能想要接受一个答案,只是可能会帮助其他遇到类似问题的人
@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();
}