Failing to get currentPage in component that's locked in the template [AEM 6.5] | Community
Skip to main content
VasconcelosAqui
Level 2
May 8, 2021
Solved

Failing to get currentPage in component that's locked in the template [AEM 6.5]

  • May 8, 2021
  • 2 replies
  • 4709 views

Hey guys, hope you doing fine.

 

I am facing a problem with getting the currentPage in a component in AEM 6.5 with SPA.

 

I am trying to get the current page inside of a model that is mapped to a component that is locked  in a template, I think if I had the component unlocked the code to get the current page would work just fine but as I really need to use that component locked I have this issue.

 

Does anyone have any sugestion to me?

 

Down bellow follows the code to get the currentPage

 

@586265
private Page currentPage;

 

public Page getCurrentPage() {
return currentPage;
}

 

the problem is that it is getting the first page from bottom up and not the correct page. As if It is returning the first that is found. And another problem that I found is that if I change the name of the attribute to another name other them currentPage, like resourcePage for example that return would be null.


If you guys have any suggestion feel free to answer here or direct message me. Thank you in advance.

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 Bhuwan_B

Please check below Code:

 

@Model(adaptables = SlingHttpServletRequest.class)
public class TestModel {

   @Inject  @Source("script-bindings")
   Page currentPage;

   public String getPath() {
       return currentPage.getPath();
   }
}

<div data-sly-use.example="com.sling.models.core.models.TestModel">

              ${example.path}<br/>

</div>

 

2 replies

Bhuwan_B
Community Advisor
Bhuwan_BCommunity AdvisorAccepted solution
Community Advisor
May 9, 2021

Please check below Code:

 

@Model(adaptables = SlingHttpServletRequest.class)
public class TestModel {

   @Inject  @Source("script-bindings")
   Page currentPage;

   public String getPath() {
       return currentPage.getPath();
   }
}

<div data-sly-use.example="com.sling.models.core.models.TestModel">

              ${example.path}<br/>

</div>

 
VasconcelosAqui
Level 2
May 10, 2021
Hey @bhuwan_b I tried this but that also didn't work, I also would like to point that that is not a simple Sling Use Model, that is actually a Model that implements component exporter and I also would like to point that the component is beeing instanciated by the template and its blocked in the the template. I think is the main reason why that does not work.
Adobe Employee
June 22, 2022

Hello @vasconcelosaqui , did you fix this? I'm having the same problem. I think it's related to those annotations. But I wasn't able to make it work. 

Even this line doesn't work because current resource is in the template and the resultant currentPage will be structure of that template.

currentPage = request.getResourceResolver().adaptTo(PageManager.class).getContainingPage(resource);
Asutosh_Jena_
Community Advisor
Community Advisor
May 9, 2021

Hi @vasconcelosaqui 

 

Looks like it's an existing issue as mentioned here:

https://github.com/adobe/aem-sample-we-retail-journal/issues/53

 

As a workaround you can use the below approach:

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

 

Thanks! 

VasconcelosAqui
Level 2
May 10, 2021
Hey @asutosh_jena_ I also tried this before but that also didnt work, cause the resource is not different for each page, as the component is locked in the template the resource is the same for every single page. I think that is the main reason why that does not work.