我有一个名为 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>
然后我将该组件嵌入到页面中:
<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
我的浏览器中的网址。
最佳答案
您应该使用正确的路径包含您的组件,否则该路径指向当前资源,并且该资源是当前页面的 jcr:content。
<div data-sly-resource="${ @path='componentPath',
resourceType='project/components/textModel'}" data-sly-unwrap></div>
关于jcr - SlingModel 未正确映射 JCR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37652615/