在aem中显示所有可用的标签

时间:2017-06-05 10:09:34

标签: java jsp tags aem

我正在尝试创建一个在crx中打印所有可用标记的组件,但我得到一个空指针异常。我创建了以下界面

package com.aem.community.core.services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public interface ListTags {
	Map listAllTags(); 
}
 和实施

package com.aem.community.core.services.Impl;

import java.util.Iterator;

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Session;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aem.community.core.services.ListTags;
import com.aem.community.core.services.SearchService;
import com.day.cq.tagging.Tag;
import java.util.HashMap;
import java.util.Map;

@Component
@Service  
public class ListTagsImpl implements ListTags {
  protected final Logger log = LoggerFactory.getLogger(this.getClass());
  
  private Session session;
  
  @Reference
  private ResourceResolver resourceResolver;
  
  @Override
  public Map listAllTags() {
    try { 
      Map<String, String> myHashMap = null;
      Resource tagResource = resourceResolver.getResource("/etc/tags");

      if (tagResource!= null) {
        Iterable<Resource> children = tagResource.getChildren();
        Iterator<Resource> itr = children.iterator();
        while (itr.hasNext()) {
          Resource item = itr.next();
          Tag tag = item.adaptTo(Tag.class);
          if (tag != null) {          	
            Iterator<Tag> childitr = tag.listAllSubTags();
            while (childitr.hasNext()) {
              Tag childitem = childitr.next();
              myHashMap.put((String)tag.getName, ((String)childitem.getName());
            }
          }
        }
      }   
      session.logout();  
      return myHashMap;
    }
    catch(Exception e) {
      log.info(e.getMessage());
    }
    return null;
  }
}

crx中的代码如下

<%@include file="/libs/foundation/global.jsp"%>
<%@page import="java.util.*, com.day.cq.tagging.* " %>
<%@page import="com.aem.community.core.services.*" %>
<%@page import="com.aem.community.core.services.Impl.*" %>
<% 
try {    
  com.aem.community.core.services.ListTags taglist = sling.getService(com.aem.community.core.services.ListTags.class);

  Map tagnames = taglist.listAllTags(); 
  out.print(tagnames);
} catch(Exception e) {
  out.print(e);
}
%>
但我无法将服务中返回的值提取到组件的jsp中。我收到了java.lang.NullPointerException

2 个答案:

答案 0 :(得分:0)

这是因为您正在尝试将ResourceResolver注入OSGI服务。你不能这样做:

@Reference
private ResourceResolver resourceResolver;

可能在您使用资源解析器的地方抛出NullPointerException。有几种方法可以在OSGI服务中获取资源解析器,但您可以参考http://www.one-inside.com/aem6-1-resourceresolver-access-in-services/http://scrupulo.com/posts/blog/view/5-tips-on-the-sling-resource-resolver

答案 1 :(得分:0)

此答案可帮助正在寻找有效遍历资源树而不是编写多个循环并对其进行调试的其他人员。 Sling API提供了称为AbstractResourceVisitor.html(AbstractResourceVisitor)的抽象实现。 ACS Commons提供了一个示例实现。Sample Implementation

TLDR:实现一个具体的类,该类扩展了AbstractResourceVisitor使用需要执行的操作覆盖visit方法。如果在继续遍历时要考虑自定义条件,则还可以覆盖accept方法。 >