Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

How to get 'currentPage' in java code when not using activate method

Avatar

Level 9

Hi All,

If we were to retrieve currentPage via activate method[seen below] , we can directly use getCurrentPage() and get it. However, in case I am not using this and also cannot pass getCurrentPage() from the java class where activate method is present , how do I get 'currentPage'?

@Override
    public void activate() throws Exception {}

Any thoughts on this will be helpful.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Activate method is for classes implementing WCMuse interface in sightly.

You can use sling model  or simple resource resolver

Page mycurrentPage = request.getResource().adaptTo(Page.class)

View solution in original post

4 Replies

Avatar

Correct answer by
Level 10

Activate method is for classes implementing WCMuse interface in sightly.

You can use sling model  or simple resource resolver

Page mycurrentPage = request.getResource().adaptTo(Page.class)

Avatar

Level 9

Hi Praveen,

Thanks a lot for your reply.

In my java code, I tried the below :

private static SlingHttpServletRequest slingRequest;

Page mycurrentPage = slingRequest.getResource().adaptTo(Page.class);

but the code is breaking after running this line and not going further.

Not sure what I am doing wrong.

Avatar

Level 10

'mycurrentPage' is object of class Page

Now you use any of the specified method available here in https://docs.adobe.com/docs/en/cq/5-6-1/javadoc/com/day/cq/wcm/api/Page.html

Please share the exception or code snippet

Avatar

Level 9

Hi Reply,

Thank you for your reply.It helped.