首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在java中的JCR节点上迭代?

如何在java中的JCR节点上迭代?
EN

Stack Overflow用户
提问于 2018-02-12 07:32:12
回答 1查看 7K关注 0票数 1

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

JCR节点如下所示

JAVA逻辑代码1:

代码语言:javascript
复制
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的系统输出如下:

代码语言:javascript
复制
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:又一次尝试,但没有运气

代码语言:javascript
复制
@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的系统:

代码语言:javascript
复制
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代码

代码语言:javascript
复制
<div data-sly-use.slide="${'SliderLogic' @ childNode='slides'}">

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

回答 1

Stack Overflow用户

发布于 2018-02-12 09:27:47

假设呈现的资源对应于slider/slides,则可以使用以下HTL片段:

代码语言:javascript
复制
<ul data-sly-list.child="${resource.listChildren}">
    <li>${child.properties.slideTitle || 'no title'}</li>
</ul>

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48741445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档