我有一项服务试图将博客页面导入CQ 5.5.0。我能够成功创建页面,但是当我添加表示内容的节点时,节点没有被保存。 CQ没有报告任何错误,我可以在创建后立即看到服务中的节点。但是当我在CRXDE Light中查看页面时,节点不是页面内容的一部分。添加节点的代码部分位于:
Node blogNode = blogPage.adaptTo(Node.class);
logOutput( INFO, "blogPage name = "+ blogPage.getName() );
// Create the author date node
Node authorDateNode = blogNode.addNode("jcr:content/authorDate", "nt:unstructured");
authorDateNode.setProperty("author", blog.getCreator());
authorDateNode.setProperty("date", sdf.format(blog.getPublishDate().getTime()));
authorDateNode.setProperty("sling:resourceType", "history/components/blog/authordate");
// Create the content node
Node blogPostNode = blogNode.addNode("jcr:content/blogPostBodyParSys", "nt:unstructured");
blogPostNode.setProperty("sling:resourceType", "history/components/parsys");
Node blogContentNode = blogNode.addNode("jcr:content/blogPostBodyParSys/text", "nt:unstructured");
blogContentNode.setProperty("sling:resourceType", "history/components/text");
blogContentNode.setProperty("text", blog.getContent());
blogContentNode.setProperty("textIsRich", "true");
// TODO: Test code only
NodeIterator itr = blogNode.getNode("jcr:content").getNodes();
while(itr.hasNext()) {
Node child = itr.nextNode();
logOutput(INFO, "Child node: " + child.getName(), 1 );
PropertyIterator propItr = child.getProperties();
while( propItr.hasNext() ) {
Property prop = propItr.nextProperty();
logOutput(INFO, "Property " + prop.getName() + ", value " + prop.getValue().getString(),2);
}
}
底部的测试代码显示新创建的节点,并按预期显示值。最后发生的事情是在服务退出之前调用'session.save'。
没有报告错误,但是当我查看页面时,我看不到节点。有没有人知道这里可能有什么问题?
答案 0 :(得分:1)
正如@Sharath Maddapa指出的那样,您需要保存会话。查看代码中所做的更改。
Node blogNode = blogPage.adaptTo(Node.class);
logOutput( INFO, "blogPage name = "+ blogPage.getName() );
// Create the author date node
Node authorDateNode = blogNode.addNode("jcr:content/authorDate", "nt:unstructured");
authorDateNode.setProperty("author", blog.getCreator());
authorDateNode.setProperty("date", sdf.format(blog.getPublishDate().getTime()));
authorDateNode.setProperty("sling:resourceType", "history/components/blog/authordate");
// Create the content node
Node blogPostNode = blogNode.addNode("jcr:content/blogPostBodyParSys", "nt:unstructured");
blogPostNode.setProperty("sling:resourceType", "history/components/parsys");
Node blogContentNode = blogNode.addNode("jcr:content/blogPostBodyParSys/text", "nt:unstructured");
blogContentNode.setProperty("sling:resourceType", "history/components/text");
blogContentNode.setProperty("text", blog.getContent());
blogContentNode.setProperty("textIsRich", "true");
//YOU must save the session here.
try {
blogNode.getSession().save();
} catch(Exception e) {// TODO Ideally log specific exceptions
logOutput( ERROR, "Error saving jcr session ");
}
// TODO: Test code only
NodeIterator itr = blogNode.getNode("jcr:content").getNodes();
while(itr.hasNext()) {
Node child = itr.nextNode();
logOutput(INFO, "Child node: " + child.getName(), 1 );
PropertyIterator propItr = child.getProperties();
while( propItr.hasNext() ) {
Property prop = propItr.nextProperty();
logOutput(INFO, "Property " + prop.getName() + ", value " + prop.getValue().getString(),2);
}
}
答案 1 :(得分:0)
我很感激输入,我终于找出了导致我问题的原因:我创建了两个ResourceResolver实例,因此我保存的会话显然与创建节点的会话不同。那次会议没有被保存。