Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Custom navigation menu using Sling Model and HTL

Avatar

Level 1

Tried building a dynamic navigation menu with a Sling Model that maps to the navigation data in AEM. In this, I tried to create a NavigationModel class that retrieves the current page and its siblings, and builds a list of NavigationItem objects that represent each page in the navigation menu.

 

So when I used @PostConstruct annotation to initialize the items list it is giving error as "method buildNavigation(Page) is undefined for the type NavigationModel" & "context cannot be resolved" and also while creating buildNavigation method which uses list of array for child pages giving "The type List is not generic; it cannot be parameterized with arguments" error.In the NavigationItem class showing error in the properties of navigation menu. The error it is throwing "methods undefined for the type Page". Attached the error screenshots for reference.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @Kiruthiga_Rajendran ,

To get the current page you can simply use this code.

@Model(adaptables = SlingHttpServletRequest.class)
public class MyModel {
    @ScriptVariable
    private Page currentPage;

    @PostConstruct
    protected void init() {
        // Your code using currentPage
    }
}

 

The code you shared where "context cannot be resolved", the context object you didn't declare. 

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

Hello @Kiruthiga_Rajendran ,

To get the current page you can simply use this code.

@Model(adaptables = SlingHttpServletRequest.class)
public class MyModel {
    @ScriptVariable
    private Page currentPage;

    @PostConstruct
    protected void init() {
        // Your code using currentPage
    }
}

 

The code you shared where "context cannot be resolved", the context object you didn't declare. 

Avatar

Community Advisor

@Kiruthiga_Rajendran 

1. you will have to make your class adaptable to SlingHttpServletRequest

and add the following-

A simpler way would be-

@Inject

private Page currentPage;

 

2. If its Resource.class you could try using the resourceresolver-

@SlingObject
private ResourceResolver resourceResolver;

@SlingObject
private Resource currentResource;

PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
pageManager.getPage(currentResource.getPath())

 

Avatar

Level 1

Hello All,

Got cleared with the previously posted query related to Java code. Now I got the new error while rendering custom navigation component in pages. Attaching the error screenshots.