aem - 无法将模型类适配到 SlingHttpServletRequest
问题描述
我正在尝试在我的模型类中注入资源。使用注释时遇到的问题@Model(adaptables = { SlingHttpServletRequest.class, Resource.class })
我得到的对象为空,而只有 Resource.Class 我得到的对象(navigationItems)。下面是我的课的片段。你能告诉我修复它的步骤吗?
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;
@Model(adaptables = { SlingHttpServletRequest.class, Resource.class })
public class Header {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Inject
@Optional
@Named("navitems")
private Resource navigationItems;
List<SiteNavigation> siteNavigationList;
@PostConstruct
protected void init() {
logger.info("In init method of header model.");
siteNavigationList = getSiteNavigationListItems(getNavigationItems());
}
private List<SiteNavigation> getSiteNavigationListItems(final Resource navigationItems, final Resource columnFourItems) {
return null;
}
public Resource getNavigationItems() {
return navigationItems;
}
}
如果我删除可选注释,我会收到以下错误:
28.09.2018 14:04:39.735 *ERROR* [0:0:0:0:0:0:0:1 [1538123679033] GET /conf/myprj/settings/wcm/templates/homepage/structure.html HTTP/1.1] com.day.cq.wcm.core.impl.WCMDeveloperModeFilter Error during include of SlingRequestPathInfo: path='/conf/myprj/settings/wcm/templates/homepage/structure/jcr:content/root/header', selectorString='null', extension='html', suffix='null'
org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: Identifier com.myprj.core.models.Header cannot be correctly instantiated by the Use API
at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:510) [org.apache.sling.scripting.core:2.0.54]
at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:552) [org.apache.sling.engine:2.6.12]
Caused by: org.apache.sling.models.factory.MissingElementsException: Could not inject all required fields into class com.myprj.core.models.Header
at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:679) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]
at org.apache.sling.models.impl.ModelAdapterFactory.internalCreateModel(ModelAdapterFactory.java:394) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]
at org.apache.sling.models.impl.ModelAdapterFactory.createModel(ModelAdapterFactory.java:261) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]
at org.apache.sling.scripting.sightly.models.impl.SlingModelsUseProvider.provide(SlingModelsUseProvider.java:126) [org.apache.sling.scripting.sightly.models.provider:1.0.6]
at org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.call(UseRuntimeExtension.java:73) [org.apache.sling.scripting.sightly:1.0.48.1_3_1]
... 243 common frames omitted
Suppressed: org.apache.sling.models.factory.MissingElementException: Could not inject private org.apache.sling.api.resource.Resource com.myprj.core.models.Header.navigationItems
at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:684) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]
... 247 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:581) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]
at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:682) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]
... 247 common frames omitted
解决方案
代码中的 3 个指针:
- 虽然 sling9 支持多种适配,但最好从SlingHttpServetlRequest 对象适配。它位于较高层并包裹大多数其他对象。
- 建议使您的注入器比通用 @Inject更具体。
- 始终指定resourceType 属性以将您的模型与特定资源类型相关联。适合片段的导出器,吊索可以关联更紧密和更好的可读性。
这将是您的吊索模型的代码:
@Model(adaptables = SlingHttpServletRequest.class, resourceType = "myprj/components/content/header",
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class Header {
@ChildResource
private Resource navitems; // Keeping resource name and attribute name identical reduces @Named annotation
推荐阅读
- c# - 获取最新上传的记录
- ios - 如果服务器端使用 FCM 发送通知,是否必须在 iOS 客户端应用程序中使用 FCM 来接收远程通知
- ember.js - 每次加载页面时的 Ember 加载变量
- c# - 如何将我的自定义菜单命令添加到 Windows 7 及更高版本中已安装的应用程序
- mysql - 获取MySQL列中至少一个为null的行数据
- java - 无效的列名 Gull
- c# - xamarin c# iOS webrequest 401错误
- c - 字符数组中的线性搜索为要搜索的元素获取垃圾值
- powershell - DSC Https 拉取服务器 - 发送请求时出错
- java - 使用 JPanel 列表制作表格