如何在cq5中基于路径创建目录?

use*_*786 2 jcr aem

我有一个String,它是页面的路径,例如/content/xperia/public/events/eventeditor.我正在创建此页面的XML并将其保存到DAM,但我想将其保存在类似的树结构下/content.

我尝试了以下代码

String page = "/content/xperia/public/events/eventeditor";
page = page.replace("/content", "/content/dam");
if (adminSession.nodeExists(page+ "/"+ "jcr:content")) {
    Node node = adminSession.getNode(page+ "/"+ "jcr:content");
    node.setProperty("jcr:data", sb.toString());                
} else {
    Node feedNode = JcrUtil.createPath(page,"nt:file", adminSession);           
    Node dataNode = JcrUtil.createPath(feedNode.getPath() + "/"+ "jcr:content", "nt:resource", adminSession);       
    dataNode.setProperty("jcr:data",sb.toString());
}
Run Code Online (Sandbox Code Playgroud)

但它给出了以下错误

找不到{ http://www.jcp.org/jcr/1.0 }内容的匹配子节点定义

因为存储库中没有这样的路径.有没有办法可以动态创建目录.由于保存此文件,我需要建立整个树xperia/public/events底下/content/dam,然后保存eventeditor.xml在该目录中.

请建议.

rak*_*110 6

您的代码存在一些问题.在JcrUtil.createPath(String absolutePath, String nodeType, Session session)创建所有给定节点类型不存在的中间路径.

这意味着所有节点xperia,public和events都是使用type nt:file而不是sling:OrderedFolder.

您可以使用该createPath(String absolutePath, boolean createUniqueLeaf, String intermediateNodeType, String nodeType, Session session, boolean autoSave)方法来指定要创建的中间节点的类型.

String page = "/content/xperia/public/events/eventeditor";
page = page.replace("/content", "/content/dam");
page += ".xml";

if (adminSession.nodeExists(page+ "/"+ "jcr:content")) {
    Node node = adminSession.getNode(page+ "/"+ "jcr:content");
    node.setProperty("jcr:data", sb.toString());                
} else {
    Node feedNode = JcrUtil.createPath(page, true, "sling:OrderedFolder", "nt:file", adminSession, false);           
    Node dataNode = feedNode.addNode("jcr:content", "nt:resource");       
    dataNode.setProperty("jcr:data",sb.toString());
}

adminSession.save();
Run Code Online (Sandbox Code Playgroud)