AEM 6.3 , get the current Page from Java 'Model' class | Community
Skip to main content
Level 4
June 20, 2017
Solved

AEM 6.3 , get the current Page from Java 'Model' class

  • June 20, 2017
  • 3 replies
  • 21614 views

Hi All,

In an AEM 6.3 project , how can I get the current Page reference from a Java 'Model' class , for example 'HelloWorldModel' (see example source below).

I would like to pass reference to current Page to Navigation (com.day.cq.wcm.foundation.Navigation).

On AEM 6.1 there was method getCurrentPage() defined in com.adobe.cq.sightly.WCMUsePojo which was the parent class for a Component bean class.

On AEM 6.3 I don't have access to getCurrentPage() from the 'Model' class (see example below).

Thanks,

Robert

----

import javax.annotation.PostConstruct;

import javax.inject.Inject;

import javax.inject.Named;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.models.annotations.Default;

import org.apache.sling.models.annotations.Model;

import org.apache.sling.settings.SlingSettingsService;

@Model(adaptables=Resource.class)

public class HelloWorldModel {

    @Inject

    private SlingSettingsService settings;

    @Inject @Named("sling:resourceType") @Default(values="No resourceType")

    protected String resourceType;

    private String message;

    @PostConstruct

    protected void init() {

        message = "\tHello World!\n";

        message += "\tThis is instance: " + settings.getSlingId() + "\n";

        message += "\tResource type is: " + resourceType + "\n";

    }

    public String getMessage() {

        return message;

    }

}

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 Feike_Visser1

2 changes you need to make:

1. make your class adaptable from SlingHttpServletRequest

2 add the following

@Inject

private Page currentPage;

3 replies

Feike_Visser1
Adobe Employee
Feike_Visser1Adobe EmployeeAccepted solution
Adobe Employee
June 20, 2017

2 changes you need to make:

1. make your class adaptable from SlingHttpServletRequest

2 add the following

@Inject

private Page currentPage;

smacdonald2008
Level 10
June 20, 2017

Feike Visser​ - any examples of this in GitHub?

Feike_Visser1
Adobe Employee
Adobe Employee
June 20, 2017
December 25, 2022

Link is showing as 404 error..