aem - 如何让 "Delegation Pattern for Sling Models"工作?

标签 aem sling-models

我正在尝试使用自定义 Sling 模型为 AEM Core 组件创建代理组件,该组件将部分功能委托(delegate)回核心组件的 Sling 模型。

我正在遵循 https://github.com/adobe/aem-core-wcm-components/wiki/Delegation-Pattern-for-Sling-Models 中的示例但它会导致 java.lang.IllegalArgumentException: Can not set com.example.core.models.Title field.

设置

  • AEM 版本 6.4.4
  • core.wcm.components.content 版本 2.4.0

来源

/apps/myproject/components/pageHeadline/.content.xml

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
    jcr:primaryType="cq:Component"
    jcr:title="Page Headline"
    jcr:description="Display Page Heading"
    sling:resourceSuperType="core/wcm/components/title/v2/title"
    componentGroup="My Project"/>

com.example.core.models.PageHeadline

package com.example.core.models;

import com.adobe.cq.wcm.core.components.models.Title;
import com.day.cq.wcm.api.Page;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Via;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.via.ResourceSuperType;

@Model(adaptables = SlingHttpServletRequest.class, adapters = Title.class, resourceType = "myproject/components/pageHeadline")
public class PageHeadline implements Title {

    @ScriptVariable
    private Page currentPage;

    @Self @Via(type = ResourceSuperType.class)
    private Title delegate;

    @Override
    public String getText() {
        return currentPage.getTitle();
    }

    @Override
    public String getType() {
        return delegate.getType();
    }
}

结果

org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: Identifier com.example.core.models.Title cannot be correctly instantiated by the Use API
...
Caused by: org.apache.sling.scripting.sightly.SightlyException: Identifier com.example.core.models.Title cannot be correctly instantiated by the Use API
    at org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.call(UseRuntimeExtension.java:78) [org.apache.sling.scripting.sightly:1.0.54.1_4_0]
    at org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl.call(RenderContextImpl.java:69) [org.apache.sling.scripting.sightly:1.0.54.1_4_0]
    at org.apache.sling.scripting.sightly.apps.core.wcm.components.title.v2.title.title_html.render(title_html.java:41)
    at org.apache.sling.scripting.sightly.java.compiler.RenderUnit.render(RenderUnit.java:48) [org.apache.sling.scripting.sightly.compiler.java:1.0.26.1_4_0]
    at org.apache.sling.scripting.sightly.impl.engine.SightlyCompiledScript.eval(SightlyCompiledScript.java:61) [org.apache.sling.scripting.sightly:1.0.54.1_4_0]
    at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:386) [org.apache.sling.scripting.core:2.0.54]
    at org.apache.sling.scripting.core.impl.DefaultSlingScript.eval(DefaultSlingScript.java:184) [org.apache.sling.scripting.core:2.0.54]
    at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:491) [org.apache.sling.scripting.core:2.0.54]
    ... 269 common frames omitted
Caused by: org.apache.sling.models.factory.MissingElementsException: Could not inject all required fields into class com.example.core.models.PageHeadline
    at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:765) [org.apache.sling.models.impl:1.4.10]
    at org.apache.sling.models.impl.ModelAdapterFactory.internalCreateModel(ModelAdapterFactory.java:448) [org.apache.sling.models.impl:1.4.10]
    at org.apache.sling.models.impl.ModelAdapterFactory.createModel(ModelAdapterFactory.java:314) [org.apache.sling.models.impl:1.4.10]
    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.54.1_4_0]
    ... 276 common frames omitted
    Suppressed: org.apache.sling.models.factory.MissingElementException: Could not inject private com.example.core.models.Title com.example.core.models.PageHeadline.title
        at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:749) [org.apache.sling.models.impl:1.4.10]
        ... 280 common frames omitted
    Caused by: org.apache.sling.models.factory.ModelClassException: Could not inject field due to reflection issues
        at org.apache.sling.models.impl.model.InjectableField.set(InjectableField.java:48) [org.apache.sling.models.impl:1.4.10]
        at org.apache.sling.models.impl.ModelAdapterFactory.setField(ModelAdapterFactory.java:989) [org.apache.sling.models.impl:1.4.10]
        at org.apache.sling.models.impl.ModelAdapterFactory.access$200(ModelAdapterFactory.java:132) [org.apache.sling.models.impl:1.4.10]
        at org.apache.sling.models.impl.ModelAdapterFactory$SetFieldCallback.inject(ModelAdapterFactory.java:500) [org.apache.sling.models.impl:1.4.10]
        at org.apache.sling.models.impl.ModelAdapterFactory.injectElement(ModelAdapterFactory.java:595) [org.apache.sling.models.impl:1.4.10]
        at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:744) [org.apache.sling.models.impl:1.4.10]
        ... 280 common frames omitted
    Caused by: java.lang.IllegalArgumentException: Can not set com.example.core.models.Title field com.example.core.models.PageHeadline.title to com.adobe.cq.wcm.core.components.internal.models.v1.TitleImpl
        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
        at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
        at java.lang.reflect.Field.set(Field.java:764)
        at org.apache.sling.models.impl.model.InjectableField.set(InjectableField.java:46) [org.apache.sling.models.impl:1.4.10]
        ... 285 common frames omitted

最佳答案

我遇到了同样的问题,委托(delegate)模式不起作用,具体来说,如果我使用 @Optional ,则注入(inject)的属性为空,或与发布的相同的堆栈跟踪 if @Optional被排除在外。

对我来说,问题是我们正在使用以下 Embed-Dependency代码:

<Embed-Dependency>
  *;scope=compile|runtime
</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>

maven-bundle-plugin 。问题在于核心组件依赖项被设置为嵌入式依赖项。一旦我解决了这个问题,委托(delegate)模式就工作得很好。

<Embed-Dependency>
  *;scope=compile|runtime;artifactId=!core.wcm.components.core
</Embed-Dependency>

关于aem - 如何让 "Delegation Pattern for Sling Models"工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56269502/

相关文章:

aem - 从jsp访问jcr中的数据

java - 吊带型号的用途

jcr - SlingModel 未正确映射 JCR

aem - 使用 Sling 模型时使用 Sling 的 AdaptTo 方法时出现错误

java - 进行 Junit 测试时,如何在 AEM Sling 模型中的节点对象中 setProperty()?

aem - aem 中的扩展、覆盖和覆盖组件有什么区别

aem - 下载 Adob​​e CQ5 的副本

user-interface - 如何增加触摸用户界面,adobe cq5 中的对话框大小?

java - 如何在rteplugins下为richtext组件添加图像插件

aem - 吊带模型 : Can I inject the SlingHttpServletRequest when adapting from Resource?