2

我有一个名为TextModel.

@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>

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

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

并通过 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。

4

2 回答 2

1

我建议使用:

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

甚至更好:

<sly data-sly-resource="${'myContent' @ resourceType='project/components/textModels'}"></sly>
于 2016-07-21T20:20:55.333 回答
1

您应该使用正确的路径包含您的组件,否则路径指向当前资源并且该资源是当前页面的 jcr:content。

<div data-sly-resource="${ @path='componentPath', 
resourceType='project/components/textModel'}" data-sly-unwrap></div>
于 2016-06-06T15:16:16.327 回答