如何迭代java中的JCR节点?

时间:2018-02-12 07:32:13

标签: java adobe jcr sightly

我在Adobe 6.2中有一个touch-ui multi-field component滑块,我无法迭代JCR中保存的多个节点,在遍历我需要创建的每个节点之后数组列表将在HTL中进一步使用。

JCR NODE如下面的截图JCR NODE

JAVA Logic code 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

Code2:另一次尝试但没有运气

@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$1@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>

2 个答案:

答案 0 :(得分:1)

使用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>

答案 1 :(得分:0)

假设您的渲染资源对应slider/slides,您可以使用以下HTL片段:

<ul data-sly-list.child="${resource.listChildren}">
    <li>${child.properties.slideTitle || 'no title'}</li>
</ul>

如果渲染的资源与此不对应,则可以利用use-model对象返回slides资源或直接返回其子节点作为列表/迭代器。