Expand my Community achievements bar.

SOLVED

Can String be converted to Node?

Avatar

Level 4

Can String be converted to Node since I do not want to use node iterator ?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

you could directly return the list from your sling model and iterate it on sightly.
No matter what kind of object it is (string or node), a data-sly-list would iterate it for you and once you hold the instance of the object, you can retrieve the values.

View solution in original post

5 Replies

Avatar

Community Advisor

Hi @chinmayis865517 ,

 

I am assuming by String you mean you have resource path to which you want to adapt as Node. One way would be is - 

 

  • Get ResourceResolver using system user and service

 

Map<String, Object> param = new HashMap<>();
param.put(ResourceResolverFactory.SUBSERVICE, "myservice");
ResourceResolver resourceResolver = resolverFactory.getServiceResourceResolver(param);

 

 

  • Use resolve method to resolve for resource path-

 

Resource res = resourceResolver.resolve("MY_PATH");

 

  • Use adaptor to adapt as Node

 

Node n = res.adaptTo(Node.class);

 

Though I would recommend to use Sling API instead of using JCR APIs until you have a strong use case of using JCR.

 

Thanks,

Ritesh Mittal

Avatar

Level 4

Hi @Ritesh_Mittal ,

 

Thank you for the reply.

 

Currently a list of string is being stored in List <String> object. This itemList is used to iterate the sly-resource of a component. There's no resource path being used . I have used currentNode and am iterating through the child nodes but the page load time has increase a lot because of this even though the solution is working.

 

Avatar

Employee Advisor

If you could tell us about this String object and what it holds(value), what are you trying to achieve, based on that we can provide code snippet.

Avatar

Level 4

Hi @DEBAL_DAS  ,

 

Thank you for the reply.

 

Currently a list of string is being stored in List <String> object. This itemList is used to iterate the sly-resource of a component. There's no resource path being used . I have used currentNode and am iterating through the child nodes but the page load time has increase a lot because of this even though the solution is working.

Avatar

Correct answer by
Community Advisor

you could directly return the list from your sling model and iterate it on sightly.
No matter what kind of object it is (string or node), a data-sly-list would iterate it for you and once you hold the instance of the object, you can retrieve the values.