0

To my understanding pageContext has access to many methods like getPage, getRequest, etc. So wouldn't it be better to simply access what you need directly as oppose to using page context? The problem is I have no idea how you would do this. So say I have this:

public void setProperties(PageContext context){
 ValueMap properties = (ValueMap) context.getAttribute("properties");
 Node currentNode = (Node) context.getAttribute("currentNode");

 pageHeader = properties.get("pageHeader", "")
}

That works fine. How would you set what you specifically need? I have this but it doesn't seem to work. I just get a **please check if the declared type is right and if the method exists.

Resource resource = requestResolver.getResource("/content/my/resource");   

public void setProperties(){

ValueMap properties = (ValueMap) resource.getAttribute("properties");
Node currentNode = (Node) resource.getAttribute("currentNode");

}
Delmon Young
  • 2,013
  • 5
  • 39
  • 51

1 Answers1

1

The key to retrieve a ValueMap is to use the Adapter framework and to adapt the appropriate Resource or Node. Both implement the Adaptable interface which enables you to adapt a Resource a number of target types like a Node, ValueMap and others. http://sling.apache.org/apidocs/sling6/org/apache/sling/api/adapter/Adaptable.html http://sling.apache.org/documentation/the-sling-engine/adapters.html

The following example shows how to retrieve a read only ValueMap

ValueMap properties = resource.adaptTo(ValueMap.class);
String propertyValue = values.get("propertyName", String.class); 

If you need to modify the contents of the ValueMap then go for the PersistableValueMap. http://sling.apache.org/apidocs/sling6/org/apache/sling/api/resource/PersistableValueMap.html

PersistableValueMap valueMap = resource.adaptTo(PersistableValueMap.class);
valueMap.put(key,value);
valueMap.save(); 
Thomas
  • 1,302
  • 9
  • 16