1

我正在尝试创建一个 Coral UI 3 多字段并使用 Sling 模型。对话框如下所示: 在此处输入图像描述

在此处输入图像描述

这是代码:

    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;
    }
}

这是 HTL 文件:

<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>

这是我得到的错误:

Caused by: org.apache.sling.models.factory.MissingElementsException: Could not inject all required fields into class com.aem.sites.models.HeaderList
    at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:593)
    at org.apache.sling.models.impl.ModelAdapterFactory.internalCreateModel(ModelAdapterFactory.java:335)
    at org.apache.sling.models.impl.ModelAdapterFactory.createModel(ModelAdapterFactory.java:223)
    at org.apache.sling.scripting.sightly.models.impl.SlingModelsUseProvider.provide(SlingModelsUseProvider.java:135)
    at org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.call(UseRuntimeExtension.java:72)
    ... 209 common frames omitted
    Suppressed: org.apache.sling.models.factory.MissingElementException: Could not inject public org.apache.sling.api.resource.Resource com.aem.sites.models.HeaderList.headerList
        at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:598)
        ... 213 common frames omitted
    Caused by: org.apache.sling.models.factory.ModelClassException: No injector returned a non-null value!
        at org.apache.sling.models.impl.ModelAdapterFactory.injectElement(ModelAdapterFactory.java:513)
        at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:596)

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

任何帮助表示赞赏。

提前致谢。

4

1 回答 1

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>
于 2017-10-02T00:45:19.900 回答