我很好奇 CQ API 中是否有允许我基于 cq:component
创建节点的内容。 ,就像作者将组件添加到 parsys 时会发生的情况一样。
因为我需要完成这件作品,所以我继续手动完成。我已经包含了这个解决方案,看看是否有人可以去“哦,伙计,你可以用这个 .* 方法来做到这一点”。
这就是我正在做的:
public static Node createFromComponent(Node dstParent, Node srcComponent, String targetName) {
Node newNode = null;
try {
//if there is a template use it
if (srcComponent.hasNode("cq:template")) {
newNode = JcrUtil.copy(srcComponent.getNode("cq:template"), dstParent, targetName);
}
else {
newNode = dstParent.addNode(targetName);
}
//set the resourceType to the path of the component sent over
newNode.setProperty("sling:resourceType", srcComponent.getPath());
newNode.getSession().save();
}
catch(Exception e) {
LOGGER.error("Error creating node from component: ", e);
}
return newNode;
}
这很直接。我在看
JcrUtil
Class ,但我认为它没有我要找的东西。
最佳答案
不幸的是,没有一个内置方法:
cq:template
的内容节点或 sling:resourceType
节点的属性。 当您将组件从 sidekick 拖到 parsys 时,您的浏览器会向 Sling 发送 HTTP POST 请求。如果目标组件包含
cq:template
子节点,此 HTTP 请求将包含一个附加属性:./@CopyFrom:/apps/my/component/cq:template
它负责设置默认值。因为您想通过 API 而不是 HTTP 请求来做同样的事情,所以您需要一个自定义方法。
关于aem - CQ API 中是否有任何内容允许您从 cq :component? 创建节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23482515/