简体   繁体   中英

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.

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. 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. 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. Just use the default properties.

properties The properties object of the current resource (org.apache.sling.api.resource.ValueMap).

Just Check if <cq:defineObjects /> is present in your JSP code or not. If you have defined CQ Objects then just use default properties object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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