0

鉴于这样的情况,作者点击发布(激活)页面然后我有以下监听器来处理事件

public class ArticleContentActivationEventHandler implements EventHandler {


private static final Logger LOGGER = LoggerFactory.getLogger(ArticleContentActivationEventHandler.class);

private static final String ARTICLE_PAGE_TEMPLATE = "/conf/myproject/settings/wcm/templates/article-template";

@Reference
private ResourceResolverService resourceResolverService;

@Override
public void handleEvent(Event event) {

    ResourceResolver resourceResolver = null;
    try {
        resourceResolver = resourceResolverService.getServiceResourceResolver();
        String resourcePath = getResourcePath(event);
        Resource resource = resourceResolver.getResource(resourcePath);

        //the resource is null, the resource path is /content/myproject/us/en, 
        //resourceResolver is not able to resolve it somehow
        for (Iterator<Resource> it = resourceResolver.getResource("/content/myproject/us/en").getChildren().iterator(); it.hasNext(); ) {
            Resource r = it.next();
            String s = r.getPath();
            String t = r.getResourceType();
        }


        if (resource.isResourceType("cq:Page")) {
            Resource jcr_content = resource.getChild("jcr:content");
            ValueMap vm = jcr_content.getValueMap();
            String template = null;
            if (vm.containsKey("cq:template")) {
                template = PropertiesUtil.toString(vm.get("cq:template"), "");
            }

下面是界面:

public interface ResourceResolverService {

ResourceResolver getServiceResourceResolver() throws LoginException;

void closeResourceResolver(ResourceResolver resourceResolver);
}

和 impl 类:

@Component(service = ResourceResolverService.class, immediate = true)
public class ResourceResolverServiceImpl implements ResourceResolverService {

    @Reference
    private ResourceResolverFactory resourceResolverFactory;

    Logger logger = LoggerFactory.getLogger(ResourceResolverServiceImpl.class);

    @Activate
    protected void activate() {
        logger.info("*** Activating Service ResourceResolverServiceImpl");
    }

    @Override
    public ResourceResolver getServiceResourceResolver() throws LoginException {
        final Map<String, Object> param = Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, (Object) "getResourceResolver");
        //the ResourceResolver returned here may got some issue?
        return resourceResolverFactory.getServiceResourceResolver(param);
    }

    @Override
    public void closeResourceResolver(ResourceResolver resourceResolver) {
        resourceResolver.close();
    }

}

我确实按照教程http://www.aemcq5tutorials.com/tutorials/resourceresolver-from-resourceresolverfactory/设置了 osgi 配置

但在我实际上甚至处理程序类中,它从未成功解析资源 resourceResolver 无法解析 /content/myproject/us/en ,resourceResolver 继续给我空值

有没有人经历过这个建议我一些代码示例来解决我的问题?谢谢

4

1 回答 1

0

检查用户映射配置,配置中使用的包 id 应该与您的项目核心工件 id 匹配。例如:在配置 bundleId = com.day.cq.wcm.cq-msm-core

或者,您可以尝试以下代码

Map<String, Object> 参数 = new HashMap<String, Object>(); param.put(ResourceResolverFactory.SUBSERVICE, "服务用户"); ResourceResolver 解析器 = resourceResolverFactory.getServiceResourceResolver(param);

于 2021-12-12T18:41:54.150 回答