如何在cq5中基于路径创建目录? [英] How to create a directory on the basis of path in cq5?

查看:73
本文介绍了如何在cq5中基于路径创建目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,它是页面的路径,例如 / content / xperia / public / events / eventeditor 。我正在整理此页面的XML并将其保存到DAM,但是我想将其保存在 / content 下的类似树结构中。

I have a String which is the path of the page for example /content/xperia/public/events/eventeditor. I am gererating the XML of this page and saving it to DAM, but I want to save it in the similar tree structure under /content.

我尝试了以下代码

I tried the following code

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());
}

但是它给出了以下错误


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

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

Because there is no such path in the repository. Is there a way through which I can create a directory on the fly. Because to save this file, I need to create the entire tree xperia/public/events under /content/dam and then save eventeditor.xml in that directory .

请建议。

推荐答案

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

There are a few issues with your code. The JcrUtil.createPath(String absolutePath, String nodeType, Session session) creates all the non-existent intermediate path with the given NodeType.

这意味着所有节点xperia,public和event的创建类型都是 nt:file 而不是 sling:OrderedFolder

This means that all the nodes xperia, public and events are created with type nt:file instead of sling:OrderedFolder.

您可以使用 createPath(字符串absolutePath,布尔createUniqueLeaf,字符串intermediateNodeType,字符串nodeType,会话会话,布尔自动保存) 方法来代替,以指定中介类型要创建的节点。

You can use the createPath(String absolutePath, boolean createUniqueLeaf, String intermediateNodeType, String nodeType, Session session, boolean autoSave) method instead, to specify the type of intermediary nodes that are to be created.

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();

这篇关于如何在cq5中基于路径创建目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆