Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Unable to get current Page Path in cloud publisher environment.

Avatar

Level 2

Hi Guys 

 

I have one requirement where i need to get a current page path to pass it into some logic in component level and that component is used in experience fragment and that fragment is used in template, template used for creating pages.

Method 1-

<header data-sly-use.header="${'com.xyz.aem.core.models.Header' @ currentPagePath = currentPage.path}"

@RequestAttribute(name="currentPagePath")
private String currentPagePath;

Method 2-

@inject
private Page currentPage;
private String currentPagePath = currentPage.getPath();

I have used these two methods in sling model, its working as expected in local instance(author/publisher) and cloud author instance but in cloud publisher environment its giving the template path. if i use the component alone in a page its working fine but the requirement is to keep that component in experience fragment and SDI used to include that experience fragment in template level

 

Steps i followed to troubleshoot.

1-cleared the SDI level cache

2-cleared the page level cache

3-used loggers to check how its behaving 

 

im not able to identify why its behaving,

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@Abhishek16 

In your SlingModel use below code to get actual page path and page resource.

String pagePath = request.getPathInfo().replace(".html", "");
Resource pageResource = resourceResolver.getResource(pagePath);

 

View solution in original post

3 Replies

Avatar

Community Advisor

@Abhishek16 you can try the below:

currentPage = request.getResourceResolver().adaptTo(PageManager.class).getContainingPage(resource);

Avatar

Level 2

Thanks for the response.

 

i used this method but getting experience fragment path, i might be using it wrong can you provide the full code if possible? 

 

 

Avatar

Correct answer by
Community Advisor

@Abhishek16 

In your SlingModel use below code to get actual page path and page resource.

String pagePath = request.getPathInfo().replace(".html", "");
Resource pageResource = resourceResolver.getResource(pagePath);