Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Aem CQ-将内容从一个页面移动到另一个页面_Aem_Jcr_Sling - Fatal编程技术网

Aem CQ-将内容从一个页面移动到另一个页面

Aem CQ-将内容从一个页面移动到另一个页面,aem,jcr,sling,Aem,Jcr,Sling,我意识到这是一个非常具体的问题,但我想以前有人碰到过这个问题。所以我有大约50页,大约是一年前创建的。我们正在尝试在页眉和页脚中添加新的组件来改进页面。除了主要内容区域中的内容将保持不变。因此,我试图将所有内容从旧页面移到新页面,但只保留主要内容区域。问题是,我不能仅仅更改旧页面上的资源类型以指向新页面组件,因为内容不同,而且页眉和页脚中会有一堆我不想要的节点。例如,以下是我当前的内容结构: 旧内容 star-trek jcr:content header

我意识到这是一个非常具体的问题,但我想以前有人碰到过这个问题。所以我有大约50页,大约是一年前创建的。我们正在尝试在页眉和页脚中添加新的组件来改进页面。除了主要内容区域中的内容将保持不变。因此,我试图将所有内容从旧页面移到新页面,但只保留主要内容区域。问题是,我不能仅仅更改旧页面上的资源类型以指向新页面组件,因为内容不同,而且页眉和页脚中会有一堆我不想要的节点。例如,以下是我当前的内容结构:

旧内容

star-trek
    jcr:content
        header
            nav
            social
            chat
        main-content
            column-one
            column-two
        footer
            sign-up
            mega-menu
star-wars
    jcr:content
        masthead
            mega-menu                                
        main-content
            column-one
            column-two
        bottom-footer
            left-links
            right-links
新内容

star-trek
    jcr:content
        header
            nav
            social
            chat
        main-content
            column-one
            column-two
        footer
            sign-up
            mega-menu
star-wars
    jcr:content
        masthead
            mega-menu                                
        main-content
            column-one
            column-two
        bottom-footer
            left-links
            right-links

有没有人知道如何只移动主内容节点中的内容,并以某种方式删除其他节点。我试图以编程的方式实现这一点,因为我不想从头开始创建50页。感谢您的帮助

确实可以编写满足您需要的代码:

package com.test;

import java.io.File;
import java.io.IOException;

import javax.jcr.ItemExistsException;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.Node;

import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.core.TransientRepository;
import org.xml.sax.SAXException;

public class test {

    public void test(Document doc) throws RepositoryException {
        try {

            // Create a connection to the CQ repository running on local host 

            Repository repository = JcrUtils
                    .getRepository("http://localhost:4502/crx/server");
            System.out.println("rep is created");

            // Create a Session
            javax.jcr.Session session = repository.login(new SimpleCredentials(
                    "admin", "admin".toCharArray()));
            System.out.println("session is created");

            String starTrekNodePath = "/content/path/";
            String starWarsNodePath = "/content/anotherPath"

            Node starTrekpageJcrNode = null;
            Node starWarstext = null;

            setProperty(java.lang.String name, Node value) 

            boolean starTrekNodeFlag = session.nodeExists(starTrekNodePath);
            boolean starWarsNodeFlag = session.nodeExists(starWarsNodePath);
            if (starTrekNodeFlag && starWarsNodeFlag) {
                System.out.println("to infinity and beyond");
                Node starTrekNode = session.getNode(starTrekNodePath);
                Node starWarsNodeFlag = session.getNode(starWarsNodePath);

                //apply nested looping logic here; to loop through all pages under this node
                //assumption is that you have similar page titles or something

                //on these lines to determine target and destination nodes
                            //2nd assumption is that destination pages exist with the component structures in q
                //once we have the target nodes, the following segment should be all you need

                Node starTrekChildNode = iterator.next();//assuming you use some form of iterator for looping logic
                Node starWarsChildNode = iterator1.next();//assuming you use some form of iterator for looping logic

                //next we get the jcr:content node of the target and child nodes

                Node starTrekChildJcrNode = starTrekChildNode.getNode("jcr:content");
                Node starWarsChildJcrNode = starWarsChildNode.getNode("jcr:content");

                // now we fetch the main-component nodes. 

                Node starTrekChildMCNode = starTrekChildJcrNode.getNode("main-content");
                Node starWarsChildMCNode = starWarsChildJcrNode.getNode("main-content");

                //now we fetch each component node

                Node starTrekChildC1Node = starTrekChildMCNode.getNode("column-one");
                Node starTrekChildC2Node = starTrekChildMCNode.getNode("column-two");

                Node starWarsChildC1Node = starWarsChildMCNode.getNode("column-one");
                Node starWarsChildC2Node = starWarsChildMCNode.getNode("column-two");

                // fetch the properties for each node of column-one and column-two from target
                String prop1;
                String prop2;
                PropertyIterator iterator = starTrekChildC1Node.getProperties(propName);
                while (iterator.hasNext()) {
                    Property prop = iterator.nextProperty();
                    prop1 = prop.getString();
                }

                PropertyIterator iterator = starTrekChildC2Node.getProperties(propName);
                while (iterator.hasNext()) {
                    Property prop = iterator.nextProperty();
                    prop2 = prop.getString();
                }



                // and now we set the values

                starWarsChildC1Node.setProperty("property-name",prop1);
                starWarsChildC2Node.setProperty("property-name",prop2);

                //end loops as appropriate
}

希望这能让你走上正轨。您必须根据
/content
中的文件夹结构,找出如何识别目标页面和目标页面,但基本逻辑应该是相同的

我认为,您可以使用JCR API随意移动内容

  • 阻止用户访问相关内容。可以使用临时ACL完成,如果可以,也可以关闭前端的访问
  • 运行使用JCR API更改内容的脚本或servlet
  • 检查结果
  • 允许用户再次访问内容
  • 对于内容修改脚本,我建议使用一个脚本来修改单个页面(即,您使用以/content/star trek.modify.txt结尾的HTTP请求来调用它),这样您就可以在单个页面上运行它,进行测试,或者在一组页面上运行它

    该脚本从当前节点开始,递归到当前节点以查找它知道如何修改的节点(基于它们的sling:resourceType),修改它们并报告它在日志或输出中所做的操作


    为了修改节点,脚本使用JCR节点API来移动对象(可能还有Worskpace.move)。

    我在这里看到的结果的问题是,您正在编写执行JCR操作以移动对象的servlet。虽然从技术上讲这是可行的,但它并不是一种真正可扩展或可重用的方法。您必须编写一些非常特定的代码,部署它,执行它,然后删除它(或者它永远存在)。这有点不切实际,也不是完全的宁静

    这里有两个更好的选择:

    我的一位同事编写了CQGroovy控制台,它使您能够使用Groovy为存储库编写更改脚本。我们经常将其用于内容转换,如您所述。使用Groovy的优点是它的脚本(不是编译/部署的代码)。如果需要,您仍然可以访问JCR API,但是控制台有一系列帮助器方法,使事情变得更加简单。我强烈推荐这种方法


    另一种方法是在AEM中使用批量编辑器工具。您可以导出内容的TSV,进行更改,然后重新导入。您必须使用管理功能打开导入功能,但我已经成功地使用了它。请注意,虽然它有点错误,但它具有数组值属性。

    感谢您的回复,并提供了一个代码示例。非常感谢。