AEM 6.3 - 使用吊索模型的多场

时间:2017-10-01 20:42:05

标签: aem sightly sling-models

我正在尝试创建一个Coral UI 3多字段并使用Sling Models。对话框如下所示: enter image description here

enter image description here

以下是代码:

library(rvest)
library(dplyr)
url <- read_html("http://relationalstocks.com/showinsiders.php?date=2017-09-15&buysell=buysell")
tb <- url %>% 
  html_node("#insidertab") %>%
  html_nodes("table") %>%
  html_table(fill = TRUE) %>%
  as.data.frame()

str(tb)
'data.frame':   253 obs. of  9 variables:
  $ Reported.Time: chr  "2017-09-15 21:00:47" "2017-09-15 20:11:26" "2017-09-15 20:11:26" "2017-09-15 20:10:27" ...
$ Tran.        : chr  "2017-09-12  Purchase" "2017-09-13  Sale" "2017-09-14  Sale" "2017-09-15  Sale" ...
$ Company      : chr  "Double Eagle Acquisition Corp." "PHIBRO ANIMAL HEALTH CORP" "PHIBRO ANIMAL HEALTH CORP" "Guidewire Software, Inc." ...
$ Ticker       : chr  "EAGL" "PAHC" "PAHC" "GWRE" ...
$ Insider      : chr  "SAGANSKY JEFFREYChief Executive Officer, Director, 10% owner" "Johnson Richard GChief Financial Officer" "Johnson Richard GChief Financial Officer" "Roza ScottChief Business Officer" ...
$ Shares.Traded: chr  "30,000" "15,900" "39,629" "782" ...
$ Avg.Price    : chr  "$10.05" "$36.46" "$36.23" "$78.20" ...
$ Value        : chr  "$301,500" "$579,714" "$1,435,758" "$61,152" ...
$ Filing       : logi  NA NA NA NA NA NA ...

这是HTL文件:

    package com.aem.sites.models;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;

import org.apache.log4j.Logger;

import com.aem.sites.models.Header;

@Model(adaptables=Resource.class)
public class HeaderList {

    final static Logger logger = Logger.getLogger(HeaderList.class);

    @Inject
    @Named("header")
    public Resource headerList;

    public List<Header> links;

    @PostConstruct
    protected void init() {
        links = new ArrayList<Header>();
        if(headerList != null) {
            logger.info("value of header list is "+headerList);
            links = populateModel(links, headerList);
        }
    }

    public  List<Header> populateModel(List<Header> array, Resource resource) {
        if(resource != null) {
            Iterator<Resource> linkResource = resource.listChildren();
            while(linkResource.hasNext()) {
                Header header = linkResource.next().adaptTo(Header.class);
                if(header != null) {
                    array.add(header);
                }
            }
        }
        return array;
    }

    public List<Header> getLinks() {
        return links;
    }

}


    package com.aem.sites.models;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;

//Creating adaptable resource class using sling annotation 
@Model(adaptables = Resource.class)
public class Header {

    @Inject
    @Named("linkText")
    private String linkText;

    @Inject
    @Named("linkUrl")
    private String linkUrl;

    @PostConstruct
    protected void init() {

    }

    public String getTitle() {
        return linkText;
    }

    public String getUrl() {
        return linkUrl;
    }

    public void setTitle(String title) {
        linkText = title;
    }

    public void setUrl(String url) {
        linkUrl = url;
    }
}

这是我得到的错误:

<header id="header" class="skel-layers-fixed">
 <nav id="nav">
 <h1><a href="#">Ion</a></h1>
<sly data-sly-use.headerObj="com.aem.sites.models.HeaderList">
<ul data-sly-list="${headerObj.items.listChildren}">
<li><a href="${item.linkUrl @context='styleToken'}">${item.linkText @context='styleToken'}</a></li>
</ul>
</sly>
</nav>
</header>

我不确定我在这里做错了什么。我不确定它是否与对话框有关?这就是我怀疑的。

感谢任何帮助。

提前致谢。

1 个答案:

答案 0 :(得分:2)

引发错误,因为模型中的属性headerList无法成功注入。您需要能够获取items节点,以便您可以列出它的子节点。也许尝试以下方法:

@Model(adaptables=Resource.class)
public class HeaderList {

    final static Logger logger = Logger.getLogger(HeaderList.class);

    @Inject
    @Named("items") <--------- changed this
    public Resource headerList;

    public List<Header> links;

    @PostConstruct
    protected void init() {
        links = new ArrayList<Header>();
        if(headerList != null) {
            logger.info("value of header list is "+headerList);
            links = populateModel(links, headerList);
        }
    }

    public  List<Header> populateModel(List<Header> array, Resource resource) {
        if(resource != null) {
            Iterator<Resource> linkResource = resource.listChildren();
            while(linkResource.hasNext()) {
                Header header = linkResource.next().adaptTo(Header.class);
                if(header != null) {
                    array.add(header);
                }
            }
        }
        return array;
    }

    public List<Header> getLinks() {
        return links;
    }
}

在您的模板中,只需使用links属性:

<sly data-sly-use.headerObj="com.aem.sites.models.HeaderList">
  <ul data-sly-list="${headerObj.links}">
    <li><a href="${item.linkUrl @context='styleToken'}">${item.linkText @context='styleToken'}</a>
    </li>
  </ul>
</sly>