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
BedrockMission!

Learn More

View all

Sign in to view all badges

How to get the current pagepath in service

Avatar

Avatar
Level 1
vijithar4145255
Level 1

Likes

0 likes

Total Posts

15 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
vijithar4145255
Level 1

Likes

0 likes

Total Posts

15 posts

Correct Reply

0 solutions
View profile
vijithar4145255
Level 1

08-02-2021

I have used the below code to get the currentpagepath in modal but if I use same code in service it is not working nd currentpage value I am getting null. Can anyone help on this.

 

@ScriptVariable
protected Page currentPage;

 

public string getCurrenPath() {

String currentPagePath = currentPage.getPath();

return currentPagePath;

}

View Entire Topic

Avatar

Avatar
Validate 1
MVP
raj_mandalapu
MVP

Likes

124 likes

Total Posts

203 posts

Correct Reply

72 solutions
Top badges earned
Validate 1
Contributor
Shape 1
Give Back 5
Give Back 3
View profile

Avatar
Validate 1
MVP
raj_mandalapu
MVP

Likes

124 likes

Total Posts

203 posts

Correct Reply

72 solutions
Top badges earned
Validate 1
Contributor
Shape 1
Give Back 5
Give Back 3
View profile
raj_mandalapu
MVP

08-02-2021

You need to pass the current page object to the OSGi service.

public void readProperties(Page currentPage) {
ResourceResolver resolver = null;
InheritanceValueMap map = new HierarchyNodeInheritanceValueMap(currentPage.getContentResource());
}

In the sling model call OSGI service

  @Inject @Source("osgi-services")
  TestService testService1;

    @PostConstruct
    public void activate(){
        System.out.println("Inside Post Constructor Method");
        testService1.readProperties(currentPage);
    }

You can also pass page path as a parameter to the service method and inside service method, you need to write something like this

@reference
private ResourceResolverFactory resolverFactory;

public void testMethod(String resourcePath ) {
try {

Resource res = resourceResolver.getResource(resourcePath);

resourceResolver.close();
} catch (LoginException e) {
// log the error
}
}