Get current page property using POJO Class. | Community
Skip to main content
Level 3
June 28, 2021
Solved

Get current page property using POJO Class.

  • June 28, 2021
  • 3 replies
  • 3984 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Ritesh_Mittal

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

 

                                 

3 replies

Ritesh_Mittal
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
June 28, 2021

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-meta-data/qaq-p/413518

Level 3
June 28, 2021

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.

Ritesh_Mittal
Community Advisor and Adobe Champion
Ritesh_MittalCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
June 28, 2021

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

 

                                 

Level 3
June 28, 2021

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."

Ritesh_Mittal
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
June 28, 2021

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

Level 3
June 28, 2021

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?