我有一个叫SlingModel的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。
发布于 2016-06-06 15:16:16
您应该将您的组件包含在正确的路径中,否则路径指向当前资源,此资源是当前页面的jcr:content。
<div data-sly-resource="${ @path='componentPath',
resourceType='project/components/textModel'}" data-sly-unwrap></div>发布于 2016-07-21 20:20:55
我建议使用:
<div data-sly-resource="${'myContent' @ resourceType='project/components/textModels'}" data-sly-unwrap></div>甚至更好:
<sly data-sly-resource="${'myContent' @ resourceType='project/components/textModels'}"></sly>https://stackoverflow.com/questions/37652615
复制相似问题