Expand my Community achievements bar.

SOLVED

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

Avatar

Level 4

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;

    }

}

1 Accepted Solution

Avatar

Correct answer by
Employee

2 changes you need to make:

1. make your class adaptable from SlingHttpServletRequest

2 add the following

@Inject

private Page currentPage;

View solution in original post

4 Replies

Avatar

Correct answer by
Employee

2 changes you need to make:

1. make your class adaptable from SlingHttpServletRequest

2 add the following

@Inject

private Page currentPage;