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
Solved! Go to Solution.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
Views
Likes
Replies