Custom navigation menu using Sling Model and HTL | Community
Skip to main content
July 21, 2023
Solved

Custom navigation menu using Sling Model and HTL

  • July 21, 2023
  • 3 replies
  • 1253 views

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.

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 Sady_Rifat

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. 

3 replies

Sady_Rifat
Community Advisor
Sady_RifatCommunity AdvisorAccepted solution
Community Advisor
July 21, 2023

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. 

Manu_Mathew_
Community Advisor
Community Advisor
July 22, 2023

@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())

 

August 2, 2023

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.