如何遍历 java 中的 JCR 节点?
How to iterate over JCR nodes in java?
我在 Adobe 6.2 中有一个 touch-ui multi-field component
用于滑块,我无法迭代保存在 JCR
中的 多个节点 ,在迭代每个节点之后节点我需要创建数组列表以在 HTL
.
中进一步使用
JCR 节点如下图所示
JAVA逻辑代码1:
public class SliderLogic extends WCMUse {
private ValueMap childProperties;
private Integer childCount;
@Override
public void activate() throws Exception {
String childNode = get("childNode", String.class);
Resource childResource = getResource().getChild(childNode);
System.out.println("childResource++++" + childResource);
if (childResource != null) {
childProperties = childResource.adaptTo(ValueMap.class);
for(Entry<String, Object> e : childProperties.entrySet()) {
String key = e.getKey();
Object value = e.getValue();
System.out.println("Elements=" + e);
System.out.println("key=" + key);
System.out.println("value=" + value);
}
}
}
public ValueMap getChildProperties() {
return childProperties;
}
}
代码1的系统输出如下:
childResource++++JcrNodeResource, type=nt:unstructured, superType=null, path=/content/zero-6/en/jcr:content/content-parsys/slider/slides
Elements=jcr:primaryType=nt:unstructured
key=jcr:primaryType
value=nt:unstructured
代码 2:再次尝试但没有成功
@Override
public void activate() throws Exception {
String childNode = get("childNode", String.class);
Resource childResource = getResource().getChild(childNode);
System.out.println("childResource++++" + childResource);
if (childResource != null) {
childProperties = childResource.adaptTo(ValueMap.class);
Iterable<Resource> getChildren = getResource().getChildren();
Iterator<Resource> children = getResource().listChildren();
System.out.println("childProperties="+childProperties);
System.out.println("getChildren="+getChildren);
while (children.hasNext()) {
Resource child = children.next();
System.out.println("child=" + child);
}
}
}
代码 2 的系统输出:
childResource++++JcrNodeResource, type=nt:unstructured, superType=null, path=/content/zero-6/en/jcr:content/content-parsys/slider/slides
childProperties=JcrPropertyMap [node=Node[NodeDelegate{tree=/content/zero-6/en/jcr:content/content-parsys/slider/slides: { jcr:primaryType = nt:unstructured, 1 = { ... }, 2 = { ... }, 3 = { ... }, 4 = { ... }}}], values={jcr:primaryType=nt:unstructured}]
getChildren=org.apache.sling.api.resource.AbstractResource@34488dc4
child=JcrNodeResource, type=nt:unstructured, superType=null, path=/content/zero-6/en/jcr:content/content-parsys/slider/bannerImage
child=JcrNodeResource, type=nt:unstructured, superType=null, path=/content/zero-6/en/jcr:content/content-parsys/slider/slides
要呈现的 HTL 代码
<div data-sly-use.slide="${'SliderLogic' @ childNode='slides'}">
<ul data-sly-list.child="${resource.listChildren}">
<li>${child} == ${child.slideTitle || 'no title'} </li>
</ul>
</div>
假设您的渲染资源对应于slider/slides
,您可以使用以下HTL片段:
<ul data-sly-list.child="${resource.listChildren}">
<li>${child.properties.slideTitle || 'no title'}</li>
</ul>
如果您呈现的资源与此不符,您可以利用 use-model 对象 return slides
资源或直接将其子节点作为 list/iterator。
使用 Apache sling 修复它,在 java 逻辑文件中创建了一个资源对象数组列表并立即使用它。
SliderLogic.java
package apps.site.components.structure.slider;
import com.adobe.cq.sightly.WCMUse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.*;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Simple utility class to help a Sightly component get a child node's properties
*/
public class SliderLogic extends WCMUse {
public ArrayList<Resource> childList = new ArrayList<Resource>();
@Override
public void activate() throws Exception {
String childNode = get("childNode", String.class);
Resource childResource = getResource().getChild(childNode);
if (childResource != null) {
Iterator<Resource> children = getResource().listChildren();
while (children.hasNext()) {
Resource child = children.next();
String parentNodeName = child.getName();
if (parentNodeName.equals("slides")) {
setChildIterator(child);
}
}
}
}
public void setChildIterator(Resource childsResourceNode) {
Iterator<Resource> childItems = childsResourceNode.listChildren();
while (childItems.hasNext()) {
Resource child = childItems.next();
childList.add(child);
}
}
public ArrayList<Resource> getChildProperties() {
System.out.println("\n items list " + childList);
return childList;
}
}
slider.html
<div data-sly-use.slide="${'SliderLogic' @ childNode='slides'}">
<ul data-sly-list.child="${slide.getChildProperties}">
<li style="color: #4bff2a">bannerImageName : ${child.bannerImageName || 'no title'} </li>
<li style="color: #ff3f10">bannerImageRef : ${child.bannerImageRef || 'no title'} </li>
<li style="color: #ff3f10">img: <img src="${child.bannerImageRef}" width="100"> </li>
<li style="color: #ebbcff">show : ${child.show || 'no title'} </li>
<li style="color: #c7a6ff">slideText : ${child.slideText || 'no title'} </li>
<li style="color: #ff19c2">slideTitle : ${child.slideTitle || 'no title'} </li>
<li style="color: #6657ff">textPosition : ${child.textPosition || 'no title'} </li>
</ul>
</div>
我在 Adobe 6.2 中有一个 touch-ui multi-field component
用于滑块,我无法迭代保存在 JCR
中的 多个节点 ,在迭代每个节点之后节点我需要创建数组列表以在 HTL
.
JCR 节点如下图所示
JAVA逻辑代码1:
public class SliderLogic extends WCMUse {
private ValueMap childProperties;
private Integer childCount;
@Override
public void activate() throws Exception {
String childNode = get("childNode", String.class);
Resource childResource = getResource().getChild(childNode);
System.out.println("childResource++++" + childResource);
if (childResource != null) {
childProperties = childResource.adaptTo(ValueMap.class);
for(Entry<String, Object> e : childProperties.entrySet()) {
String key = e.getKey();
Object value = e.getValue();
System.out.println("Elements=" + e);
System.out.println("key=" + key);
System.out.println("value=" + value);
}
}
}
public ValueMap getChildProperties() {
return childProperties;
}
}
代码1的系统输出如下:
childResource++++JcrNodeResource, type=nt:unstructured, superType=null, path=/content/zero-6/en/jcr:content/content-parsys/slider/slides
Elements=jcr:primaryType=nt:unstructured
key=jcr:primaryType
value=nt:unstructured
代码 2:再次尝试但没有成功
@Override
public void activate() throws Exception {
String childNode = get("childNode", String.class);
Resource childResource = getResource().getChild(childNode);
System.out.println("childResource++++" + childResource);
if (childResource != null) {
childProperties = childResource.adaptTo(ValueMap.class);
Iterable<Resource> getChildren = getResource().getChildren();
Iterator<Resource> children = getResource().listChildren();
System.out.println("childProperties="+childProperties);
System.out.println("getChildren="+getChildren);
while (children.hasNext()) {
Resource child = children.next();
System.out.println("child=" + child);
}
}
}
代码 2 的系统输出:
childResource++++JcrNodeResource, type=nt:unstructured, superType=null, path=/content/zero-6/en/jcr:content/content-parsys/slider/slides
childProperties=JcrPropertyMap [node=Node[NodeDelegate{tree=/content/zero-6/en/jcr:content/content-parsys/slider/slides: { jcr:primaryType = nt:unstructured, 1 = { ... }, 2 = { ... }, 3 = { ... }, 4 = { ... }}}], values={jcr:primaryType=nt:unstructured}]
getChildren=org.apache.sling.api.resource.AbstractResource@34488dc4
child=JcrNodeResource, type=nt:unstructured, superType=null, path=/content/zero-6/en/jcr:content/content-parsys/slider/bannerImage
child=JcrNodeResource, type=nt:unstructured, superType=null, path=/content/zero-6/en/jcr:content/content-parsys/slider/slides
要呈现的 HTL 代码
<div data-sly-use.slide="${'SliderLogic' @ childNode='slides'}">
<ul data-sly-list.child="${resource.listChildren}">
<li>${child} == ${child.slideTitle || 'no title'} </li>
</ul>
</div>
假设您的渲染资源对应于slider/slides
,您可以使用以下HTL片段:
<ul data-sly-list.child="${resource.listChildren}">
<li>${child.properties.slideTitle || 'no title'}</li>
</ul>
如果您呈现的资源与此不符,您可以利用 use-model 对象 return slides
资源或直接将其子节点作为 list/iterator。
使用 Apache sling 修复它,在 java 逻辑文件中创建了一个资源对象数组列表并立即使用它。
SliderLogic.java
package apps.site.components.structure.slider;
import com.adobe.cq.sightly.WCMUse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.*;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Simple utility class to help a Sightly component get a child node's properties
*/
public class SliderLogic extends WCMUse {
public ArrayList<Resource> childList = new ArrayList<Resource>();
@Override
public void activate() throws Exception {
String childNode = get("childNode", String.class);
Resource childResource = getResource().getChild(childNode);
if (childResource != null) {
Iterator<Resource> children = getResource().listChildren();
while (children.hasNext()) {
Resource child = children.next();
String parentNodeName = child.getName();
if (parentNodeName.equals("slides")) {
setChildIterator(child);
}
}
}
}
public void setChildIterator(Resource childsResourceNode) {
Iterator<Resource> childItems = childsResourceNode.listChildren();
while (childItems.hasNext()) {
Resource child = childItems.next();
childList.add(child);
}
}
public ArrayList<Resource> getChildProperties() {
System.out.println("\n items list " + childList);
return childList;
}
}
slider.html
<div data-sly-use.slide="${'SliderLogic' @ childNode='slides'}">
<ul data-sly-list.child="${slide.getChildProperties}">
<li style="color: #4bff2a">bannerImageName : ${child.bannerImageName || 'no title'} </li>
<li style="color: #ff3f10">bannerImageRef : ${child.bannerImageRef || 'no title'} </li>
<li style="color: #ff3f10">img: <img src="${child.bannerImageRef}" width="100"> </li>
<li style="color: #ebbcff">show : ${child.show || 'no title'} </li>
<li style="color: #c7a6ff">slideText : ${child.slideText || 'no title'} </li>
<li style="color: #ff19c2">slideTitle : ${child.slideTitle || 'no title'} </li>
<li style="color: #6657ff">textPosition : ${child.textPosition || 'no title'} </li>
</ul>
</div>