In Sling Model, what is the equivalent of ValueMapValue(name = "jcr:title", optional = true) | Adobe Higher Education
Skip to main content
jydps87387977
Level 3
March 10, 2016
解決済み

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

  • March 10, 2016
  • 3 の返信
  • 3849 ビュー

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

このトピックへの返信は締め切られました。
ベストアンサー smacdonald2008

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. 

3 の返信

Jitendra_S_Toma
Level 10
March 10, 2016

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

jydps87387977
jydps87387977作成者
Level 3
March 10, 2016

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

smacdonald2008
Level 10
March 10, 2016

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.