Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Get current page property using POJO Class.

Avatar

Level 3

I wish to access the current page jcr:content property using POJO class and read properties of current page specifically cq:tags and display that in my html page.

Please help me with the creating POJO class code for it and then accessing it in HTML.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @ShagunMalik ,

 

Using below code you can access any property and can set in Sling Model Getter, which you can access in HTL-

 

Map<String, Object> param = new HashMap<>();
param.put(ResourceResolverFactory.SUBSERVICE, "myservice");
try (ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(param)) {

         Resource res = resolver.getResource("PATH to RESOURCE");

         ValueMap pageProperties = resource.getValueMap();

          String propertyValue = properties.get("property name", String[].class);

 

 

Now, your question about what is subservice-

 

Just think of subservice as "Level of Access". Means you are trying to get repository access and you define which service I want to use, for example

 

1. I want to write content to repository, so I need write access so my service name could be writeService (name of service can be anything meaningful)

2. I just want to read some content and no need to write so I can use same writeService (not reccommended) or can create new one with least privileges required.

 

Now, using Apache Sling User Mapper Service, we just Map this Sub service to system user. system user is actually the one which has read/write access and we use sub service name to map it with system user.

 

One more thing, there are implicit out of the objects, which you can also use if you want to access the current page properties, you can refer below URL-

 

https://experienceleague.adobe.com/docs/experience-manager-htl/using/htl/global-objects.html?lang=en

 

                                 

View solution in original post

4 Replies

Avatar

Community Advisor

Hi @ShagunMalik ,

 

you can check this recent thread, using Sling Model and Resource Resolver through subservice, you can easily achieve it.

 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/want-to-use-tags-to-create...

Avatar

Level 3

Hey @Ritesh_Mittal , earlier I was providing specific path but now I wish to access jcr:content properties of each current page. Please explain how to do that.

Avatar

Level 3
And I watched the video link you provided for Subservice but I'm not clear about what is SubService and why is it used. Can you please explain if possible?

Avatar

Correct answer by
Community Advisor

Hi @ShagunMalik ,

 

Using below code you can access any property and can set in Sling Model Getter, which you can access in HTL-

 

Map<String, Object> param = new HashMap<>();
param.put(ResourceResolverFactory.SUBSERVICE, "myservice");
try (ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(param)) {

         Resource res = resolver.getResource("PATH to RESOURCE");

         ValueMap pageProperties = resource.getValueMap();

          String propertyValue = properties.get("property name", String[].class);

 

 

Now, your question about what is subservice-

 

Just think of subservice as "Level of Access". Means you are trying to get repository access and you define which service I want to use, for example

 

1. I want to write content to repository, so I need write access so my service name could be writeService (name of service can be anything meaningful)

2. I just want to read some content and no need to write so I can use same writeService (not reccommended) or can create new one with least privileges required.

 

Now, using Apache Sling User Mapper Service, we just Map this Sub service to system user. system user is actually the one which has read/write access and we use sub service name to map it with system user.

 

One more thing, there are implicit out of the objects, which you can also use if you want to access the current page properties, you can refer below URL-

 

https://experienceleague.adobe.com/docs/experience-manager-htl/using/htl/global-objects.html?lang=en

 

                                 

Avatar

Level 3

Hey @Ritesh_Mittal , getting error "Map cannot be resolved to a type",

"HashMap cannot be resolved to a type",

"'<>' operator is not allowed for source level below 1.7" and "Resource Specification not allowed for source level below 1.7" and "resourceResolverFactory acnnot be resolved."

Avatar

Community Advisor

Hi @ShagunMalik ,

 

You need to import all required classes in your Java class. Also, diamond <> operator requires JDK 1.8, if you already have 1.8 then it means in your eclipse you are using maven version as 1.7. Just check in pom.xml.

 

Your class should have all imports like below-

https://github.com/riteshmittal/multiple-subservices-system-users/blob/main/MyServiceFirstImpl.java

Avatar

Level 3

Hey @Ritesh_Mittal , I just checked my Eclipse is using version as 1.7 and I cannot change it due to policies. can you tell me how do the same task using Node iterator?

Avatar

Level 3
Hey @Ritesh_Mittal, can we achieve same task using Node iterator? If yes then please help how.

Avatar

Community Advisor

@ShagunMalik , I dint get, which policies? You can change the maven version via going to file system and open the file in any editor and change.

Avatar

Level 3
@Ritesh_Mittal, project policies. Cannot go in depth of it due to confidentiality. Can we achieve the same task using Nodes and Node Iterator?