AEM 6.3 - 使用 Sling 模型的多场
AEM 6.3 - Multifield using Sling Model
我正在尝试创建一个 Coral UI 3 multifield 并使用 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)
我不确定我做错了什么。我不确定它是否与对话框有关?这就是我所怀疑的。
感谢任何帮助。
提前致谢。
报错是因为你模型中的属性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>
我正在尝试创建一个 Coral UI 3 multifield 并使用 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)
我不确定我做错了什么。我不确定它是否与对话框有关?这就是我所怀疑的。
感谢任何帮助。
提前致谢。
报错是因为你模型中的属性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>