【问题标题】:Adobe Experience Manager - How can we get currentPage url into DatasourceAdobe Experience Manager - 我们如何将 currentPage url 获取到 Datasource
【发布时间】:2017-03-24 10:41:22
【问题描述】:

下面是 datasource.jsp,用于将动态下拉列表添加到我的 customComponent 对话框中,我在我的 customComponent 中使用这个数据源,如下面提到的字段。 在这里,我的要求是当我的 customComponent 在任何页面(此页面具有 url)中使用时,需要将 url 值获取到下拉列表。所以在这里我需要使用我的 customComponent 的 currentPage url。

请帮助我获取我使用 customComponent 的页面 url 到这个数据源。

<%
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 对话框 content.xml 中,它使用上述数据源,因为它是 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>

【问题讨论】:

    标签: aem


    【解决方案1】:

    SlingHttpServletRequest(一般来说)提供了SlingBindings的实例,其中包含对"currentPage"的引用(我使用的是静态字段WCMBindings.CURRENT_PAGE [dependency: groupId: com.adobe.cq.sightly, artifactId: @987654326 @,版本: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);
    }
    

    【讨论】:

    • 感谢您的回复。我对 AEM 非常陌生,您能了解一下它是如何工作的吗?
    【解决方案2】:

    在数据源的resourceType代码中,您可以通过读取CONTENTPATH属性并使用资源解析器获取资源来获取当前页面的URL,然后您可以将其适配到Node中。该节点将是对话框的内容项。确认在 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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多