How to get the current pagepath in service

Avatar

Avatar

vijithar4145255

Avatar

vijithar4145255

vijithar4145255

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

raj_mandalapu

MVP

Avatar

raj_mandalapu

MVP

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
}
}