我正在尝试创建自己的图像句柄。该类使用用户定义的类 MySmartImage.java,该类具有 com.day.cq.wcm.foundation.Image,以及两个字符串属性及其 getter setter,如下所示。
public class MySmartImage extends WCMUse {
private Image image;
private String titleText;
private String descText;
@PostConstruct
public void activate() {
}
public MySmartImage(Resource resource) {
this.image = new Image(resource);
}
public MySmartImage(Image image, String titleText, String descText) {
this.image = image;
this.titleText = titleText;
this.descText = descText;
}
public MySmartImage(Resource resource, String titleText, String descText) {
this.image = new Image(resource);
this.titleText = titleText;
this.descText = descText;
}
public Image getImage() {
return image;
}
public String getTitleText() {
return "test";
}
public void setImage(Image image) {
this.image = image;
}
public void setTitleText(String titleText) {
this.titleText = titleText;
}
public void setDescText(String descText) {
this.descText = descText;
}
public String getDescText() {
return descText;
}
}
我的处理程序如下:
嗨,我现在在处理程序中实现了 getImages,如下所示。
公共类 ImageHandler 扩展 WCMUse {
private String param;
private static Resource resource;
private Test test;
private List<MySmartImage> images = new ArrayList<MySmartImage>();
@Override
public void activate() throws Exception {
resource = getResource();
test = new Test();
//images = this.getImages();
//param = get("param", String.class);
}
public List<MySmartImage> getImages() {
System.out.println("Resouce ::::" + resource);
List<MySmartImage> images = new ArrayList<MySmartImage>();
Resource imagesResource = resource.getChild("images");
System.out.println("Images ::::" + images);
if (imagesResource == null) {
return images;
}
ValueMap map = imagesResource.adaptTo(ValueMap.class);
String order = map.get("order", String.class);
if (order == null) {
return images;
}
System.out.println("Order :::: " + order);
JSONArray array; ValueMap vMap;
try {
array = new JSONArray(order);
} catch (JSONException e) {
array = new JSONArray();
}
for (int i = 0; i < array.length(); i++) {
String imageResourceName;
try {
imageResourceName = array.getString(i);
} catch (JSONException e) {
imageResourceName = null;
}
System.out.println("image resource name :::: " + imageResourceName);
if (imageResourceName != null) {
Resource imageResource = imagesResource.getChild(imageResourceName);
System.out.println("imageResource :::: " + imageResource);
if (imageResource != null) {
Iterator childImagesItr = imageResource.listChildren();
System.out.println("childImagesItr :::: " + childImagesItr);
while(childImagesItr.hasNext()){
Resource childImage = (Resource)childImagesItr.next();
Image image = new Image(childImage);
image.setItemName(Image.PN_REFERENCE,"imageReference");
image.setSelector("img");
image.setAlt(childImage.getName());
vMap = imageResource.adaptTo(ValueMap.class);
String title = vMap.get("titleText", String.class);
String desc = vMap.get("descText", String.class);
MySmartImage img = new MySmartImage(childImage);
img.setTitleText(title);
img.setDescText(desc);
images.add(img);
System.out.println("image added in images :::: " + images.size());
}
}
}
}
System.out.println("image added in images :::: " + images.getClass());
return images;
}
}
因此,当在我的视觉页面上,我试图按如下方式访问图像处理程序。
<div data-sly-use.handle="ImageHandler">
<div data-sly-list.image="${handle.images }">
-----${image.titleText}
</div>
</div>
它给出了以下异常
org.apache.sling.scripting.sightly.SightlyException:在 org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl.getObjectNoArgMethod(RenderContextImpl.java:350) 在 org.apache.sling 的 java.lang.NullPointerException .scripting.sightly.impl.engine.runtime.RenderContextImpl.getObjectProperty(RenderContextImpl.java:323) 在 org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl.getProperty(RenderContextImpl.java:281) 在 org .apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl.resolveProperty(RenderContextImpl.java:103) 在 apps.gbp.components.content.c_stage2.SightlyJava_c_stage2.render(SightlyJava_c_stage2.java:54) 在 org.apache .sling.scripting.sightly.impl.engine.runtime.RenderUnit.render(RenderUnit.java:52) 在 org.apache.sling.scripting.sightly.impl.engine。SightlyScriptEngine.evaluateScript(SightlyScriptEngine.java:95) 在 org.apache.sling.scripting.sightly.impl.engine.SightlyScriptEngine.eval(SightlyScriptEngine.java:83)