Can String be converted to Node? | Community
Skip to main content
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
B_Sravan
B_SravanAccepted solution
New Participant
Jan 31, 2023 7:58 AM UTC

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.

5 replies

DEBAL_DAS
New Participant
Jan 31, 2023 5:20 AM UTC

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.

Translate
Level 4
Jan 31, 2023 6:05 AM UTC

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.

Translate
B_Sravan
B_SravanAccepted solution
New Participant
Jan 31, 2023 7:58 AM UTC

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.

Translate
Ritesh_Mittal
New Participant
Jan 31, 2023 4:31 AM UTC

Hi @chinmayish ,

 

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

Translate
Level 4
Jan 31, 2023 6:04 AM UTC

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.

 

Translate