在JCR节点中保存数据,我做错了什么?

时间:2014-04-03 05:20:31

标签: java cq5 jcr sling

我已成功创建并部署了一个捆绑包(Servlet),接受用户的用户名和密码,现在我想将它保存在/ content / mydata /下的JCR Repository中 我得到了例外

java.lang.IllegalArgumentException: relPath is not a relative path: {}  {}oliver

这是我的代码

  public class CustomerJCRAccessImp implements CustomerService {
        @Reference
        protected SlingRepository repository;

    protected static final Logger log = LoggerFactory.getLogger(CustomerJCRAccessImp.class);

    public void insertData(String username, String password) throws Exception {

        log.error("Username ::"+username+" Password ::"+password);
        log.error("XXX:: Inside the Service Method");
        Session session=    repository.loginAdministrative(null);
        Node node= session.getRootNode();
        Node contentNode = node.getNode("content");
        //node.i
        Node  myAppNode = contentNode.getNode("myApp");
        log.error("THE VALUE OF myApp NODE ::"+myAppNode);


        Node user = myAppNode.addNode("/"+username);
        user.setProperty("Roll No", "1");
        user.setProperty("Age", "10");
        user.setPrimaryType("nt:unstructured");

        session.save();
        session.logout();




    }
    protected void bindRepository(SlingRepository repository) {
        this.repository = repository; 
    }
}

我通过引用此链接完成了此操作 http://helpx.adobe.com/experience-manager/using/persisting-cq-data-java-content.html 在此先感谢。

1 个答案:

答案 0 :(得分:5)

addNode()方法的相对路径参数不应以“/”开头。 试试

Node user = videojetNode.addNode(username);

虽然我同意文档中的术语“ relPath ”非常容易引起误解,但relPath应该是您要在当前节点下创建的节点的名称,或者应该从子节点的名称,并包含要在其下创建节点的目标节点的相对路径。

例如。如果当前节点是 content 并且您有以下树

/
|_content
    |_x
       |_y

如果您希望将名为z的节点添加为y的子节点,则可以将relPath指定为

Node myNode = contentNode.addNode("x/y/z");

注意:如果任何中间节点不可用,将抛出PathNotFoundException