代码之家  ›  专栏  ›  技术社区  ›  Alex R

javascript中的jcr_sql2示例?

  •  -2
  • Alex R  · 技术社区  · 7 年前

    我正在服务器端寻找一些使用aem jcr_sql2 api的示例代码 Javascript ( 不是Java ,即以 use(function() { ... }) 并通过 data-sly-use=${...} 是的。

    所有google结果都是100%基于java的示例。

    我已经尝试过:google“jcr-sql2 js示例”和变体。

    预期结果:javascript中的示例代码。

    实际结果:大量Java代码:-(

    2 回复  |  直到 7 年前
        1
  •  4
  •   Alexander Berndt    7 年前

    如果您想使用服务器端js(我不推荐),那么您只需要转换java示例的语法。无论如何,您都可以与java对象交互。所以js和java的api是一样的。如果您有一个htl组件并通过use api调用js,那么您的js范围中已经定义了几个对象。

    https://helpx.adobe.com/experience-manager/htl/using/global-objects.html

    下面是一个用sql-2查询搜索所有核心组件的js示例:

    use(function () {
        var pageName = currentPage.name;
        var title = currentPage.properties.get("jcr:title");
        var resourceName = granite.resource.name;
        var resourceTitle = properties.get("jcr:title");
    
        var componentList = [];
        var componentIter = resolver.findResources("SELECT * FROM [cq:Component] AS c WHERE ISDESCENDANTNODE(c, '/apps/core/wcm')", "JCR-SQL2");
        while (componentIter.hasNext()) {
            var compoenentRes = componentIter.next();
            componentList.push(compoenentRes.getPath());
        }
    
        return {
            pageName: pageName,
            title: title,
            resourceName: resourceName,
            componentList: componentList
        };
    });
    

    使用它的组件htl代码是:

    <div data-sly-use.info="info.js">
        <p>page name: ${info.pageName}</p>
        <p>title: ${info.title}</p>
        <p>resourceName: ${info.resourceName}</p>
        <p>core components: </p>
        <ul data-sly-list.component="${info.componentList}">
            <li>${component}
        </ul>
    </div>
    

    PS:你可能知道,但这里有JS的使用API: https://helpx.adobe.com/experience-manager/htl/using/use-api-javascript.html

        2
  •  0
  •   Alexander Berndt    7 年前

    我不知道任何用于jcr sql2查询的web api。您必须在aem(使用java)中实现自己的servlet,它将接受来自外部http请求的查询。然后,可以通过ajax从js代码调用servlet,并使用java在aem中执行查询。

    也许是 查询生成器API 对你来说很有趣。这种查询语言已经可以从外部获得,并且可以通过ajax调用。为了测试和开发查询,可以使用查询调试器:

    Adobe文档:

    https://helpx.adobe.com/experience-manager/6-3/sites/developing/using/querybuilder-api.html

    查询调试器:

    http://localhost:4502/libs/cq/search/content/querydebug.html

    带有示例查询的查询调试器:

    http://localhost:4502/libs/cq/search/content/querydebug.html? charset =UTF-8&query=type%3Dcq%3APage%0D%0Aorderby%3D%40jcr%3Acontent%2Fcq%3AlastModified%0D%0Aorderby.sort%3Ddesc

    直接调用的示例查询:

    http://localhost:4502/bin/querybuilder.json?orderby=%40jcr%3acontent%2fcq%3alastModified&orderby.sort=desc&type=cq%3aPage

    推荐文章