Good to Know: The Sling PersistableValueMap

Published on by Dan KlcoPicture of Me Dan Klco
This was also posted on:, however 6D Global Technologies decided to remove my authoring credit, which seems unethical.

The Sling ValueMap makes retrieving properties from CQ easy and removes a lot of the error-prone code you have to use when using the JCR APIs, however it does not allow setting of properties. This unfortunately results in developers using the Sling ValueMap to retrieve properties and JCR Nodes and Properties to set properties.

This doesn’t have to be the case though! Sling also provides an interface which allows for retrieving properties like a ValueMap and an easy faculty for setting properties as well. The PersistableValueMap differs from the ValueMap only in it’s put method does not throw an exception and it offers two more methods. The reset method, which resets any values set in the current session and the save method, which persists any values set in the current session.

Like the ValueMap, any Sling Resource can be adapted to a PersistableValueMap, to retrieve a PersistableValueMap, simply call:

PersistableValueMap propertes = aResource.adaptTo(PersistableValueMap.class);

Once you have an instance of the PersistableValueMap, you can use it like a ValueMap

String value = propertes.get("key",String.class);

Or use it to persist values to CQ:



comments powered by Disqus