aemslingsling-models

Creating Sling Models from resource paths


I would like to instantiate a Sling Model from a JSP using a path to a resource like

<sling:adaptTo adaptable="/path/to/my/resource"  adaptTo="org.apache.sling.models.it.models.MyModel" var="model"/>

Within the the Sling Model, I would like to access properties of the specified resource via @ValueMapValue annotations.

My question is, how can I adapt my Sling Model to a resource path String so that I can inject the properties of the specified resource?


Solution

  • The sling:getResource EL Function provided by Sling could be used in conjunction with the sling:adaptTo tag which you are endevoring to use:

    <sling:adaptTo adaptable="${sling:getResource(resourceResolver,'/path/to/my/resource')}" adaptTo="org.apache.sling.models.it.models.MyModel" var="model"/>
    

    Full documentation concerning the available tags can be found on the Sling site.