问题描述
我正在使用 lombok 的委托模式 - 希望父超类型的所有 getter 方法都将在 model.json 中导出 这是我为Tabs组件写的sling模型
package com.test.internal.models;
import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.export.json.ContainerExporter;
import com.adobe.cq.export.json.ExporterConstants;
import com.adobe.cq.wcm.core.components.internal.models.v1.TabsImpl;
import com.adobe.cq.wcm.core.components.models.Component;
import com.adobe.cq.wcm.core.components.models.Container;
import com.adobe.cq.wcm.core.components.models.ListItem;
import com.adobe.cq.wcm.core.components.models.Tabs;
import com.adobe.cq.wcm.core.components.models.datalayer.ComponentData;
import lombok.experimental.Delegate;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.*;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import org.apache.sling.models.annotations.via.ResourceSuperType;
import javax.inject.Inject;
import java.util.List;
@Model(
adaptables = {Resource.class,SlingHttpServletRequest.class},adapters = {Tabs.class,ComponentExporter.class,ContainerExporter.class,Container.class},resourceType = Accordion.RESOURCE_TYPE
)
@Exporter(
name = ExporterConstants.SLING_MODEL_EXPORTER_NAME,extensions = ExporterConstants.SLING_MODEL_EXTENSION
)
public class Accordion implements Tabs {
static final String RESOURCE_TYPE = "test/components/accordion";
@Self @Via(type = ResourceSuperType.class)
@Delegate(types = Tabs.class,excludes = DelegationExclusion.class)
private com.adobe.cq.wcm.core.components.models.Tabs delegate;
@Override
public String getActiveItem() {
return "test";
}
private interface DelegationExclusion { // Here we define the methods we want to override
String getActiveItem();
}
}
我正在尝试覆盖 Tabs 组件的 getActiveItem 方法。但它没有正确填充项目 - 它们在 json 中是空的 - 只是在 json 中返回覆盖的方法 -
"accordion": {
"items": [],"activeItem": "test",":itemsOrder": [],":items": {}
}
当我从捆绑包中移除这个 Sling 模型时 - OOTB sling 模型出现并返回所有内容 -
"accordion": {
"id": "accordion-5248ba2449","activeItem": "item_1",":itemsOrder": [
"item_1","item_2","ctabutton"
],":items": {
"item_1": {
"gridClassNames": "aem-Grid aem-Grid--12 aem-Grid--default--12","columnClassNames": {
"backbutton": "aem-GridColumn aem-GridColumn--default--12"
},"columnCount": 12,"allowedComponents": {
"components": [],"applicable": false
},":itemsOrder": [
"backbutton"
],":items": {
"backbutton": {
":type": "test/components/backbutton","btnLabel": "View More"
}
},":type": "test/components/container","cq:panelTitle": "FAQ 1"
},"item_2": {
"gridClassNames": "aem-Grid aem-Grid--12 aem-Grid--default--12","columnClassNames": {},":items": {},"cq:panelTitle": "FAQ 2"
},"ctabutton": {
":type": "test/components/ctabutton","btnAlignment": "left","lightBoxId": "sadasdasdasdasdas","btnType": "cta-primary","btnLabel": "View More","openLightBox": "true","cq:panelTitle": null
}
},":type": "test/components/accordion"
}
有人可以让我知道吊索模型缺少什么,它不能正确返回物品
解决方法
这是项目中的工作示例,应该对您有所帮助。
@Model(
adaptables = SlingHttpServletRequest.class,adapters = { Tabs.class,ComponentExporter.class,ContainerExporter.class },resourceType = absModel.TABS_RESOURCE_TYPE,defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
@Exporter(
name = ExporterConstants.SLING_MODEL_EXPORTER_NAME,extensions = ExporterConstants.SLING_MODEL_EXTENSION
)
@JsonSerialize(as = TabsModel.class)
public class TabsModel implements Tabs {
protected static final String TABS_RESOURCE_TYPE = "myproject/components/tabs";
@Self
private SlingHttpServletRequest request;
@Self
@Via(type = ResourceSuperType.class)
@Delegate(excludes = Excludes.class)
private Tabs tabs;
@Override
public String getExportedType() {
return this.request.getResource().getResourceType();
}
private interface Excludes {
String getExportedType();
}
}