Java 字段的值未显示在 aem 组件中

Values of Java fields are not shown in aem component

我有 aem 组件和 Sling 模型,其中三个注入字段对应于组件中的对话框字段。只有 headline 字段的值显示在组件上。我尝试将其他字段的类型更改为文本字段,但没有任何效果。我没有发现我的字段之间有任何其他差异。

这里是Java class:

@Model(
    adaptables = {SlingHttpServletRequest.class},
    defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
public class ArticlePreviewImpl implements ArticlePreview {
  @Inject
  private String contentpath;
  
  @Inject
  private String headline;
  
  @Inject
  private String elements;
  
  @Override
  public String getContentpath() {
    return contentpath;
  }
  
  @Override
  public String getHeadline() {
    return headline;
  }
  
  @Override
  public String getElements() {
    return elements;
  }
}

这是 HTL:

<!--/* articlepreview.html */-->
<div class="cmp-apreview"
     data-sly-use.apreview="com.training.core.models.ArticlePreview">
        path: ${apreview.contentPath}<br/>
        headline: ${apreview.headline}<br/>
        elements: ${apreview.elements}
</div>
<sly data-sly-use.clientlib="/libs/granite/sightly/templates/clientlib.html"></sly>
<sly data-sly-call="${clientlib.js @ categories='cq.authoring.dialog, training.components'}"></sly>

以下是对话框字段的定义。content.xml:

<contentPath
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/pathfield"
fieldLabel="Path to directory with content"
name="./contentpath"/>
...
<headline
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
emptyText="Enter the headline to display."
fieldLabel="Headline"
name="./headline"/>
...
<elements
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/numberfield"
defaultValue="3"
fieldDescription="Number of elements in one portion"
fieldLabel="Number of elements in one portion"
max="{Double}15.0"
min="{Double}1.0"
name="./elements"
value="3"/>

组件上仅显示 apreview.headline 的值,其他两个未显示。

对于 ${apreview.contentPath},您需要将 getter 更改为 getContentPath(注意骆驼 Case)。对于 ${apreview.elements},getter 看起来没问题,您需要检查 属性 是否在 JCR 中为您的资源正确填充(因为 DefaultInjectionStrategy.OPTIONAL 在注入期间允许空字段) .

这非常简单和愚蠢 - 我将 HTL 推入存储库而不是构建整个项目