【问题标题】:SlingModel not mapping the JCR properlySlingModel 未正确映射 JCR
【发布时间】:2016-10-05 18:39:04
【问题描述】:

我有一个名为 TextModel 的 SlingModel。

@Model(adaptables=Resource.class, defaultInjectionStrategy= DefaultInjectionStrategy.OPTIONAL)
public class TextModel {

    @Inject
    private String heading;

    @Inject
    private String description;

    public String getHeading() {
        return heading;
    }

    public String getDescription() {
        return description;
    }
}

我在 Sightly 中也有一个渲染组件的模板:

<div data-sly-use.model="project.components.slingmodels.text.TextModel" data-sly-unwrap/>
<div>
    <p>PageModel component</p>
    <h1>${model.heading}</h1>
    <p>Description: ${model.description}</p>
</div>

然后我将组件嵌入到一个页面中:

&lt;div data-sly-resource="${@ resourceType='project/components/textModel'}" data-sly-unwrap&gt;&lt;/div&gt;

并通过 JSON 创建初始 JCR 结构:

{
    "jcr:primaryType": "nt:unstructured",
    "sling:resourceType": "project/pages/page",
    "title" : "Welcome page",
    "jcr:content" : {
        "myContent" : {
            "jcr:primaryType": "nt:unstructured",
            "sling:resourceType" : "project/components/textModel",
            "heading": "Heading",
            "description": "test description"
        }
    }
}

所有字段都正确保存在 JCR 中,但我的 Sling 模型返回 null 作为 headingdescription 的值。

但是,当我创建这样的内容时:

{
    "jcr:primaryType": "nt:unstructured",
    "sling:resourceType": "project/pages/page",
    "title" : "Welcome page",
    "heading": "Heading",
    "description": "test description",
    "jcr:content" : {
        "myContent" : {
            "jcr:primaryType": "nt:unstructured",
            "sling:resourceType" : "project/components/textModel"
        }
    }
}

它有效。 JSON 存储在我的项目文件中的jcr_root/content/hello.json 下,我正在浏览器中打开一个localhost:8080/content/hello.html URL。

【问题讨论】:

    标签: jcr sling sightly sling-models


    【解决方案1】:

    你应该用正确的路径包含你的组件,否则路径指向当前资源,这个资源就是当前页面的 jcr:content。

    <div data-sly-resource="${ @path='componentPath', 
    resourceType='project/components/textModel'}" data-sly-unwrap></div>
    

    【讨论】:

      【解决方案2】:

      我建议使用:

      <div data-sly-resource="${'myContent' @ resourceType='project/components/textModels'}" data-sly-unwrap></div>
      

      甚至更好:

      <sly data-sly-resource="${'myContent' @ resourceType='project/components/textModels'}"></sly>
      

      【讨论】:

        猜你喜欢
        • 2020-08-02
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-08
        • 1970-01-01
        相关资源
        最近更新 更多