Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

How to use a Sling Model to adapt a Resource defined in an Editable Template?

Level 4
Level 4

I have a simple sling model, the important part looks like this:

This works fine if I use HTL's <data-sly-use="..."> in a component to access resources under the page where the component is used.

However, if my component is used in the structure of an editable template, this breaks since the resources I want are in the template node, not the page node.

Is there a way to access the template resource node instead?

4 Replies
Level 10
Level 10

You can bind nodes in the AEM JCR like this. Create a Resource that points to the node in the AEM JCR - an editable template in your example:

Resource resource = resolver.getResource("/content/testsling/slingmodel");

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

  response.getWriter().write("Output from ValueMap is First Name: "+valueMap.get("firstName").toString()+" Last Name: "+valueMap.get("lastName").toString()+" Technology: "+valueMap.get("technology").toString()+"");

  UserInfo userInfo = resource.adaptTo(UserInfo.class);

  response.getWriter().write("Output from Sling Model is First Name: "+userInfo.getFirstName()+" Last Name: "+userInfo.getLastName()+" Technology: "+userInfo.getTechnology());

Using this - you can base a Sling Model on JCR nodes.

Adobe Experience Manager Help | Creating Adobe Experience Manager 6.3 Sling Model Components

Hope this helps....           

Community Advisor
Community Advisor


Try to adapt sling model with {SlingHttpServletRequest.class} and use @ValueMapValue annotation over fields.



Level 4
Level 4

I looked into @ValueMapValue and @Via but did not see anything that would automatically "walk" the JCR back to the template. As a work-around I hard-coded a JCR path as in smacdonald2008​'s suggestion, but would like to refactor this for better maintainability/re-use.