【问题标题】:programmatically setting cq:tags save blank value in the node in AEM以编程方式设置 cq:tags 在 AEM 的节点中保存空白值
【发布时间】:2020-01-30 12:32:16
【问题描述】:

我正在尝试以编程方式从 json 字符串中提取数据,转换为字符串数组并将其作为 cq:tags 属性和相应的值添加到节点中,但是当我这样做时,虽然 cq:tags 属性是已添加但具有空白值。

我的节点是这样的:/content/<my project node>/ContentPage/jcr:content

        ResourceResolver resolver = CommonUtils.getResourceResolver(resourceResolverFactory);

  String[] strValue = tagList.stream().toArray(String[]::new); // tagList has String values in form of array.
            Resource resource = resolver.getResource(CONTENT_DATA_NODE);
            if (resource != null) {
                Node node = resource.adaptTo(Node.class);
                if (node != null) {
                    NodeIterator nodeIterator = node.getNodes();
                    while (nodeIterator.hasNext()) {
                        innerNode = nodeIterator.nextNode();
                        innerNode.setProperty(CQ_TAGS, strValue);
                        innerNode.getSession().save();
                    }
                }
            }

我的 sling 用户映射器服务是 mybundle.core:datawrite=userdatawriter ,如果我的资源解析器工厂为空,我会直接从请求中获取解析器。

最初,我认为这可能是访问问题,因此我以编程方式尝试使用任何随机属性和值: 属性:xyz,值:aa,bb,cc,dd 这是由我的代码编写的,没有任何问题, 只有在以编程方式添加 cq:tags 时才会出现问题。尽管我可以从页面属性或 crxde 节点本身手动添加具有任何长值列表的 cq:tags ,而不会出现任何问题。

我在这里遗漏了什么并且在代码中做错了,它不仅可以添加 cq:tags 还可以覆盖如果 cq:tags 存在。

P.S:我的 AEM 版本是 AEM 6.5 SP2

【问题讨论】:

    标签: aem aem-6


    【解决方案1】:

    我可以在 AEM 6.4.3 中看到同样的情况。保存属性后,立即可以按预期读取该值。这是我在AEM Groovy console 中运行的一些简单示例。

    def node = getNode('/content/screens/we-retail/apps/virtual-showroom/en/jcr:content')
    String[] arr = ['a', 'b', 'c']; 
    String[] tagArr  = ['we-retail:equipment', 'we-retail:activity/biking']
    node.setProperty('foo', arr)
    println node.getProperty('foo').values // prints the a, b,c tags
    node.setProperty('cq:tags', tagArr)
    session.save()
    println node.getProperty('cq:tags').values // prints the a, b,c tags
    println node.getProperty('foo').values // prints the a, b,c tags
    

    但是,在 CRXDE 中检查页面时,我可以看到该属性为空。当您使用的值与 AEM 中的现有标签匹配时,不会发生这种情况。例如:

    def node = getNode('/content/screens/we-retail/apps/virtual-showroom/en/jcr:content')
    String[] arr = ['a', 'b', 'c'];
    String[] tagArr  = ['we-retail:equipment', 'we-retail:activity/biking']
    node.setProperty('foo', arr)
    println node.getProperty('foo').values
    node.setProperty('cq:tags', tagArr)
    println node.getProperty('cq:tags').values // prints the we-retail tags
    session.save()
    println node.getProperty('foo').values
    println node.getProperty('cq:tags').values // prints the we-retail tags
    

    并且在 CRXDE 中可以看到相同的值。

    我相信,这种行为是由Day CQ Tagging Service (com.day.cq.tagging.impl.JcrTagManagerFactoryImpl)

    控制的

    取消选中该框将禁用验证并允许您保留这些值。但是,使用不存在的标签标记页面将导致其自身的问题。相反,我建议确保在使用它们之前创建这些标签。

    【讨论】:

    • 但取消选中标签验证将使内容无法使用标签进行搜索,这将否定添加标签的整个想法。
    • @Ajay,您如何搜索此内容?您似乎正在尝试使用 AEM 中不存在的标签,这比禁用此验证更有可能导致搜索问题。
    • 动态地首先保存在 /content/cq:tags 中,然后读取它并设置到 cq:tags 页面道具下的页面中是一件昂贵的事情。
    • @Ajay 你不必动态地这样做。您可以事先准备正确的分类并上传。您尝试使用的标签名称来自哪里?
    猜你喜欢
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 2011-02-14
    • 2017-05-13
    相关资源
    最近更新 更多