简体   繁体   English

吊索 - 获取资源的属性

[英]Sling - Get properties of a resource

I'm really new to sling so I apologize in advance. 我真的很新,所以我提前道歉。 I've got a simple script that I just can't get to work. 我有一个简单的脚本,我无法工作。 All I want to do is get the "lastModified" property of a specific resource located in the JCR. 我想要做的就是获取位于JCR中的特定资源的“lastModified”属性。

Resource getResource = resourceResolver.getResource("/content/AboutPage/jcr:content/list");
ValueMap properties = resource.adaptTo(ValueMap.class);
String lastModified = properties.get("jcr:lastModified", String.class);

Instead all I get is the error below: 相反,我得到的是以下错误:

Duplicate local variable properties

Any help is greatly appreciated! 任何帮助是极大的赞赏! Thanks! 谢谢!

"Duplicate local variable" in Java or JSP code simply means there's already a variable with this name in the same scope. Java或JSP代码中的“重复局部变量”仅意味着在同一范围内已存在具有此名称的变量。 If you didn't define that variable yourself, you probably included some other code that does. 如果您没有自己定义该变量,那么您可能会包含其他一些代码。

As you noticed, you just need to change the variable's name to avoid the issue. 正如您所注意到的,您只需更改变量的名称即可避免此问题。

Instead of 代替

ValueMap properties = resource.adaptTo(ValueMap.class);

put: 放:

ValueMap properties = getResource.adaptTo(ValueMap.class);

I just realized what I was doing wrong. 我刚刚意识到我做错了什么。 If I change "properties" to "property" it seems to work. 如果我将“属性”更改为“属性”,它似乎有效。 I guess you can't adapt a value map to "properties". 我猜你不能将值映射调整为“属性”。

Resource getResource = resourceResolver.getResource("/content/AboutPage/jcr:content/list");
ValueMap property = resource.adaptTo(ValueMap.class);
String lastModified = property.get("jcr:lastModified", String.class);

There is properties defined in CQ Taglibs. CQ Taglibs中定义了properties Check this link That is why you are getting a duplicate variable error. 检查此链接这就是您收到重复变量错误的原因。

Moreover, you do not need to create your own properties if you really are using CQ tablibs. 此外,如果您确实使用CQ tablib,则无需创建自己的属性。 Just use the default properties. 只需使用默认属性即可。

properties The properties object of the current resource (org.apache.sling.api.resource.ValueMap). properties当前资源的属性对象(org.apache.sling.api.resource.ValueMap)。

Just Check if <cq:defineObjects /> is present in your JSP code or not. 只检查JSP代码中是否存在<cq:defineObjects /> If you have defined CQ Objects then just use default properties object. 如果已定义CQ对象,则只使用默认属性对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM