AEM 6.2页面属性继承及Live Copy使用问题咨询
解决AEM 6.2 Live Copy中克隆页面需手动设置模板和ResourceType的问题
我完全理解你的场景——用Live Copy来实现首页导航复用,同时允许其他页面随时断开关联改用自定义配置,这个思路本身非常契合AEM MSM的设计初衷。那两个手动改属性的痛点确实烦人,尤其是批量克隆页面的时候,下面给你两个实用的解决方案:
方案1:通过自定义Post-Rollout Action自动同步属性
这是最灵活的方式,能确保每次克隆页面后自动把源页面的cq:template和sling:resourceType同步到目标Live Copy页面,完全不用手动操作。
你只需要写一个简单的OSGI组件,实现AEM MSM的PostAction接口,在rollout完成后执行属性复制逻辑:
import com.day.cq.wcm.msm.api.PostAction; import com.day.cq.wcm.msm.api.RolloutManager; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.osgi.service.component.annotations.Component; import javax.jcr.Node; import javax.jcr.RepositoryException; @Component(service = PostAction.class, property = { RolloutManager.PROPERTY_POST_ACTION_NAME + "=copyTemplateAndResourceType", RolloutManager.PROPERTY_POST_ACTION_LABEL + "=Copy Template and Resource Type", RolloutManager.PROPERTY_POST_ACTION_DESCRIPTION + "=Automatically copies cq:template and sling:resourceType from source to live copy page" }) public class TemplateResourceTypeCopyAction implements PostAction { @Override public void execute(Resource sourceResource, Resource targetResource, String[] rolloutConfigNames, ResourceResolver resolver) throws RepositoryException { if (sourceResource == null || targetResource == null) { return; } Resource sourceContent = sourceResource.getChild("jcr:content"); Resource targetContent = targetResource.getChild("jcr:content"); if (sourceContent == null || targetContent == null) { return; } Node sourceNode = sourceContent.adaptTo(Node.class); Node targetNode = targetContent.adaptTo(Node.class); // 复制cq:template属性 if (sourceNode.hasProperty("cq:template")) { targetNode.setProperty("cq:template", sourceNode.getProperty("cq:template").getString()); } // 复制sling:resourceType属性 if (sourceNode.hasProperty("sling:resourceType")) { targetNode.setProperty("sling:resourceType", sourceNode.getProperty("sling:resourceType").getString()); } resolver.commit(); } }
把这个组件打包部署到你的AEM实例后,在创建Live Copy的Rollout配置里启用这个Post Action,之后所有通过该配置克隆的页面都会自动继承源页面的模板和资源类型。
方案2:通过MSM Rollout配置实现属性复制(无需代码)
如果你的团队不想写自定义代码,也可以直接通过AEM自带的MSM配置来实现:
- 进入Tools > Sites > Live Copy > Rollout Configurations
- 创建一个新的Rollout配置,或者编辑你正在使用的配置
- 切换到Property Replication标签页,点击"Add Rule"添加两条规则:
- 第一条:路径填
jcr:content/cq:template,操作选择Copy - 第二条:路径填
jcr:content/sling:resourceType,操作选择Copy
- 第一条:路径填
- 保存配置后,在创建Live Copy时选择这个配置,克隆后的页面就会自动带上这两个属性
额外提示:保留灵活修改的能力
不管用哪种方案,都不会影响你原本的需求——当需要给某个页面设置自定义导航时,只需要在页面的MSM工具栏里选择Break Inheritance,针对导航相关的属性断开关联,之后就能自由修改该页面的配置了,完全不影响其他Live Copy页面。
内容的提问来源于stack exchange,提问作者Hai Hoang
相关产品推荐
相关产品推荐

