Expand my Community achievements bar.

SOLVED

In Sling Model, what is the equivalent of ValueMapValue(name = "jcr:title", optional = true)

Avatar

Level 4

Hi,

Again a question about the Sling Model, are the equivalent

1). 

  final Resource jcrResource = resource.getChild("jcr:content");
        final ValueMap valueMap = jcrResource.adaptTo(ValueMap.class);
        final String title = valueMap.get("jcr:title", ""); // GETTING THE TITLE POPULATED WITH SOME VALUE

OR 

2). 
@ValueMapValue(name = "jcr:title", optional = true)
    private String title; // TITLE IS NOT GETTING POPULATED

1 Accepted Solution

Avatar

Correct answer by
Level 10

The article shows you the difference between the Sling Models and using ValueMaps:

  Resource resource = resourceResolver.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()+"");
             
  org.kalyan.poc.sling.models.UserInfo userInfo = resource.adaptTo(org.kalyan.poc.sling.models.UserInfo.class);
   response.getWriter().write("Output from Sling Model is First Name: "+userInfo.getFirstName()+" Last Name: "+userInfo.getLastName()+" Technology: "+userInfo.getTechnology());
                     
 

The article shows you how to map to node properties using Sling Models. 

View solution in original post

3 Replies

Avatar

Level 9

Have you tried like this?.

@Model(adaptables=Resource.class)public class MyModel {@Inject @Named("jcr:title") //@Named to be used if property name & Model variable having different name.private String titleProp; }

---Jitendra

Avatar

Level 4

Thank you Jitendra.

But this is exactly what I am trying. 

@Inject @Named("jcr:title")private String title;

And the following works(but I need to use the Sling Model pattern)

final Resource jcrResource = resource.getChild("jcr:content");
        final ValueMap valueMap = jcrResource.adaptTo(ValueMap.class);
        final String title = valueMap.get("jcr:title", ""); // GETTING THE TITLE POPULATED WITH SOME VALUE

Avatar

Correct answer by
Level 10

The article shows you the difference between the Sling Models and using ValueMaps:

  Resource resource = resourceResolver.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()+"");
             
  org.kalyan.poc.sling.models.UserInfo userInfo = resource.adaptTo(org.kalyan.poc.sling.models.UserInfo.class);
   response.getWriter().write("Output from Sling Model is First Name: "+userInfo.getFirstName()+" Last Name: "+userInfo.getLastName()+" Technology: "+userInfo.getTechnology());
                     
 

The article shows you how to map to node properties using Sling Models.