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

Fetch browser url and split the url and get the page names and urls for parent page.

Avatar

Level 3

Hi Team

 

I would like to retrieve the current url on dispatcher and split it based on suffix and get name & url for the current parent pages.

 

For ex: https://experienceleaguecommunities.adobe.com/t5/forums/postpage/board-id/adobe-experience-manager-q...

This is the url which I am hitting, my request is to get this url and get substring after the host name like /t5/forums/postpage/board-id/adobe-experience-manager-qanda

 

Here Again I will split this into 5 substrings and get page name and url for each.

 

1. /t5 -- get page name (jcr:title) and page url

2. /forums -- get page name (jcr:title) and page url

3. /postpage-- get page name (jcr:title) and page url

4. /board-id -- get page name (jcr:title) and page url

5. /adobe-experience-manager-ganda -- get page name (jcr:title) and page url

 

Request you to kindly provide reference for creating such sling model.

 

Thanks and Regards

Prashanthi

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @Prashardan ,

If I understand correctly your model will be like this,

@Model(adaptables = {Resource.class, SlingHttpServletRequest.class})
public class PageInfo {
    @ScriptVariable
    private Page currentPage;
    @Self
    private SlingHttpServletRequest request;

    @PostConstruct
    private void init() {
        // get the page content path
        // /t5/forums/postpage/board-id/adobe-experience-manager-qanda
        String path = "/t5/forums/postpage/board-id/adobe-experience-manager-qanda";
        String[] pathSegments = path.split("/");
        // String[] pathSegments = currentPage.getPath().split("/"); // or any logic here
        
        // Iterate over the each part
        for (int i = 0; i < pathSegments.length; i++) {
            // get the page
            Page page = pageManager.getPage(pathSegments[i]);

            String pageTitle = page.getTitle(); // jcr:title
            String pageURL = request.getResourceResolver().map(page.getPath()); // make page URL

            // Write your logic based on your requirement, ex: Put in a Arraylist
            
        }
    }
}

Or, you can also try this way

@Model(adaptables = {Resource.class, SlingHttpServletRequest.class})
public class PageInfo {
    @ScriptVariable
    private Page currentPage;
    @Self
    private SlingHttpServletRequest request;

    @PostConstruct
    private void init() {
        // get the page content path
        // /t5/forums/postpage/board-id/adobe-experience-manager-qanda
        String path = "/t5/forums/postpage/board-id/adobe-experience-manager-qanda";
        int segmentLength = path.split("/").length;

        // Know your hidden path depth
        // /content/project-name/country/language
        // 0 - 1 - 2 - 3
        
        // Iterate over the each part
        for (int i = segmentLength; i > 3; i--) { // 3 is the depth of hidden path that is hide by dispather
            // get the page
            Page page = currentPage.getAbsoluteParent(i);

            String pageTitle = page.getTitle(); // jcr:title
            String pageURL = request.getResourceResolver().map(page.getPath()); // make page URL

            // Write your logic based on your requirement, ex: Put in a Arraylist
            
        }
    }
}

Just which one you prefer.

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hello @Prashardan ,

If I understand correctly your model will be like this,

@Model(adaptables = {Resource.class, SlingHttpServletRequest.class})
public class PageInfo {
    @ScriptVariable
    private Page currentPage;
    @Self
    private SlingHttpServletRequest request;

    @PostConstruct
    private void init() {
        // get the page content path
        // /t5/forums/postpage/board-id/adobe-experience-manager-qanda
        String path = "/t5/forums/postpage/board-id/adobe-experience-manager-qanda";
        String[] pathSegments = path.split("/");
        // String[] pathSegments = currentPage.getPath().split("/"); // or any logic here
        
        // Iterate over the each part
        for (int i = 0; i < pathSegments.length; i++) {
            // get the page
            Page page = pageManager.getPage(pathSegments[i]);

            String pageTitle = page.getTitle(); // jcr:title
            String pageURL = request.getResourceResolver().map(page.getPath()); // make page URL

            // Write your logic based on your requirement, ex: Put in a Arraylist
            
        }
    }
}

Or, you can also try this way

@Model(adaptables = {Resource.class, SlingHttpServletRequest.class})
public class PageInfo {
    @ScriptVariable
    private Page currentPage;
    @Self
    private SlingHttpServletRequest request;

    @PostConstruct
    private void init() {
        // get the page content path
        // /t5/forums/postpage/board-id/adobe-experience-manager-qanda
        String path = "/t5/forums/postpage/board-id/adobe-experience-manager-qanda";
        int segmentLength = path.split("/").length;

        // Know your hidden path depth
        // /content/project-name/country/language
        // 0 - 1 - 2 - 3
        
        // Iterate over the each part
        for (int i = segmentLength; i > 3; i--) { // 3 is the depth of hidden path that is hide by dispather
            // get the page
            Page page = currentPage.getAbsoluteParent(i);

            String pageTitle = page.getTitle(); // jcr:title
            String pageURL = request.getResourceResolver().map(page.getPath()); // make page URL

            // Write your logic based on your requirement, ex: Put in a Arraylist
            
        }
    }
}

Just which one you prefer.

Avatar

Community Advisor

The easiest way would be to use a servlet instead a sling model and pass the "page URL" via a parameter so you can manipulate it in the backend. 

 

The problem with a sling model is that you cannot fully get the URL you are seeing in the browser, because this may be transformed and rewritten by other means (dispatcher rewrites, redirects, etc). So, although you can get the currentPage and use externalizer within slingModel unless you have all the other means' logic you won't be able to fully re-create the "pageURL" you are seeing in the browser in the backend.

 

That being said, just add a js call to your servlet where you add the window.location.href as a parameter, here you have an example of how to do it: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/passing-path-parameters-in... 

 



Esteban Bustamante