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

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

Avatar

Avatar
Give Back
Level 2
VasconcelosAqui
Level 2

Likes

7 likes

Total Posts

27 posts

Correct Reply

0 solutions
Top badges earned
Give Back
Ignite 1
Validate 1
Boost 5
Boost 3
View profile

Avatar
Give Back
Level 2
VasconcelosAqui
Level 2

Likes

7 likes

Total Posts

27 posts

Correct Reply

0 solutions
Top badges earned
Give Back
Ignite 1
Validate 1
Boost 5
Boost 3
View profile
VasconcelosAqui
Level 2

08-05-2021

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

 

@inject
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.

components Sling Model SPA

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Affirm 25
Level 5
Bhuwan_B
Level 5

Likes

90 likes

Total Posts

90 posts

Correct Reply

32 solutions
Top badges earned
Affirm 25
Establish
Give Back
Boost 50
Boost 5
View profile

Avatar
Affirm 25
Level 5
Bhuwan_B
Level 5

Likes

90 likes

Total Posts

90 posts

Correct Reply

32 solutions
Top badges earned
Affirm 25
Establish
Give Back
Boost 50
Boost 5
View profile
Bhuwan_B
Level 5

08-05-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>

 

Answers (1)

Answers (1)

Avatar

Avatar
Give Back 100
Level 10
asutosh_jena
Level 10

Likes

551 likes

Total Posts

663 posts

Correct Reply

191 solutions
Top badges earned
Give Back 100
Boost 500
Affirm 100
Ignite 1
Establish
View profile

Avatar
Give Back 100
Level 10
asutosh_jena
Level 10

Likes

551 likes

Total Posts

663 posts

Correct Reply

191 solutions
Top badges earned
Give Back 100
Boost 500
Affirm 100
Ignite 1
Establish
View profile
asutosh_jena
Level 10

08-05-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!