我有一个名为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 作为 和 的heading
值description
。
但是,当我创建这样的内容时:
{
"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。