Adobe Experience Manager - 我们如何将currentPage网址添加到数据源

时间:2017-03-24 10:41:22

标签: cq5 aem

下面是datasource.jsp,用于将动态下拉列表添加到我的customComponent对话框中,我在customComponent中使用此数据源作为下面提到的字段。 在我的customComponent用于任何页面(此页面包含url)时,我的要求是需要将url值设置为下拉列表。所以这里我需要currentPage url,其中我使用了我的customComponent。

请帮助我获取该页面网址,我将customComponent用于此数据源。

<%
request.setAttribute(DataSource.class.getName(), EmptyDataSource.instance()); 
ResourceResolver resolver = resource.getResourceResolver();
//Create an ArrayList to hold data
List<Resource> fakeResourceList = new ArrayList<Resource>();
ValueMap vm = null;
Resource childResource = resourceResolver.getResource(currentPage.getPath()+"/jcr:content/node/path");
if(childResource!=null){
    Node childNode = childResource.adaptTo(Node.class);
    Node childLinks = childNode.getNode("childnode");
     if(childLinks!=null){    
    NodeIterator childrenNodes = childLinks.getNodes();
          while(childrenNodes.hasNext()) {
             vm = new ValueMapDecorator(new HashMap<String, Object>());
             Node next = childrenNodes.nextNode();
             String label = next.getProperty("label").getValue().getString();
             String path = next.getProperty("url").getValue().getString();
             vm.put("text",label);
             vm.put("value",path.substring(1));       
             fakeResourceList.add(new ValueMapResource(resolver, new ResourceMetadata(), "nt:unstructured", vm)); 
         }
   }
} else {
    vm = new ValueMapDecorator(new HashMap<String, Object>());
    vm.put("text","NoValue");
    vm.put("value","");
    fakeResourceList.add(new ValueMapResource(resolver, new ResourceMetadata(), "nt:unstructured", vm));
}
DataSource ds = new SimpleDataSource(fakeResourceList.iterator());
request.setAttribute(DataSource.class.getName(), ds);
%>

在我的customComponent对话框中,使用上面的datasource作为它的sling:resourceType。

<dataSourceTest
     jcr:primaryType="nt:unstructured"
     sling:resourceType="granite/ui/components/foundation/form/select"
     fieldDescription="Provide ID"
     fieldLabel="Anchor"
     name="./datasourceTest">
     <datasource
          jcr:primaryType="nt:unstructured"
          sling:resourceType="/apps/mysite/components/page/datasource"/>
</dataSourceTest>

2 个答案:

答案 0 :(得分:0)

SlingHttpServletRequest(通常)提供SlingBindings的实例,其中包含对"currentPage"的引用(我使用静态字段WCMBindings.CURRENT_PAGE [dependency:groupId:在我的示例中,com.adobe.cq.sightly,artifactId:cq-wcm-sightly-extension,版本:1.2.30]。

我在我的示例中使用的Optional是一个Java 8类,可用于避免对null引用进行过多检查。

final Optional<Page> optional = Optional.ofNullable(request)
        .map(req -> (SlingBindings) req.getAttribute(SlingBindings.class.getName()))
        .map(b -> (Page) b.get(WCMBindings.CURRENT_PAGE));

简化/显式示例

Page getCurrentPageFromRequest(@Nonnull final SlingHTTPRequest request) {
  final SlingBindings bindings = (SlingBindings) request.getAttribute(SlingBindings.class.getName())
  if (bindings == null) {
    return null;
  }
  return (Page) bindings.get(WCMBindings.CURRENT_PAGE);
}

答案 1 :(得分:0)

在数据源的resourceType代码中,您可以通过读取CONTENTPATH属性并使用资源解析器获取当前页面的URL来获取可以适应节点的资源。该节点将是该对话框的内容项。确认在AEM 6.2中工作。

// get list of child nodes of the current component node
Resource res = resourceResolver.getResource((String) request.getAttribute(Value.CONTENTPATH_ATTRIBUTE));
Node node = res.adaptTo(Node.class);
log.info(node.getPath());

来源:https://rmengji.wordpress.com/2016/07/16/aem-6-2-touch-ui-dropdown-pulling-data-dynamically-using-sightly/