Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.

currentPage is null on model adaptable to SlingHttpServletRequest.class and Resource.class

Avatar

Level 2

Hi,

I'm trying to understand how injectors work on models, so i created the following scenario:

Sling Models Injectors:
script-bindings - org.apache.sling.models.impl.injectors.BindingsInjector
valuemap - org.apache.sling.models.impl.injectors.ValueMapInjector
child-resources - org.apache.sling.models.impl.injectors.ChildResourceInjector
request-attributes - org.apache.sling.models.impl.injectors.RequestAttributeInjector
define-objects - com.adobe.acs.commons.models.injectors.impl.AemObjectsInjector
osgi-services - org.apache.sling.models.impl.injectors.OSGiServiceInjector

I created an adaptable class to both SlingHttpServletRequest.class  AND Resource.class :

@Model(adaptables = {SlingHttpServletRequest.class , Resource.class})
public class ModelTest {

    protected final Logger logger = LoggerFactory.getLogger(ModelTest.class);

    @Inject
    @Optional
    private String propertyTest;

    @Inject
    @Optional
    private Page currentPage;

    @PostConstruct
    protected void init() {
        logger.debug("Is propertyTest null? {}", propertyTest == null);
        logger.debug("Is currentPage null? {}",currentPage == null);
    }
}

I'm getting the following result:

18.11.2016 11:06:05.655 *DEBUG* [0:0:0:0:0:0:0:1 [1479427565418] GET /content/test/en/model-test.html HTTP/1.1] com.test.models.ModelTest Is propertyTest null? false
18.11.2016 11:06:05.655 *DEBUG* [0:0:0:0:0:0:0:1 [1479427565418] GET /content/test/en/model-test.html HTTP/1.1] com.test.models.ModelTest Is currentPage null? true

Changing adaptable to SlingHttpServletRequest ONLY:

@Model(adaptables = SlingHttpServletRequest.class)
public class ModelTest {

    protected final Logger logger = LoggerFactory.getLogger(ModelTest.class);

    @Inject
    @Optional
    private String propertyTest;

    @Inject
    @Optional
    private Page currentPage;

    @PostConstruct
    protected void init() {
        logger.debug("Is propertyTest null? {}", propertyTest == null);
        logger.debug("Is currentPage null? {}",currentPage == null);
    }
}


I'm getting the following result:

18.11.2016 11:08:05.733 *DEBUG* [0:0:0:0:0:0:0:1 [1479427565418] GET /content/test/en/model-test.html HTTP/1.1] com.test.models.ModelTest Is propertyTest null? true
18.11.2016 11:08:05.733 *DEBUG* [0:0:0:0:0:0:0:1 [1479427565418] GET /content/test/en/model-test.html HTTP/1.1] com.test.models.ModelTest Is currentPage null? false

Why i can't inject both objects in my model at the same time when i declare my model adaptable to both SlingHttpServletRequest.class and Resource.class? 

2 Replies

Avatar

Employee

The currentPage is only available via the request, not via the resource.

Avatar

Community Advisor

@Gustavo, there is absolutely nothing in AEM that prevents you from either extending/improving Sling Models with the code that would allow you to retrieve Page from Resource.

When your SlingModel is created you can get and set your Resource to your local variable.

Later in your @PostConstruct you can adapt your Resource to whatever else it natively adaptTo()

Alternatively, if you are feeling geeky, you could implement your custom annotation to do the same.[1]

[1] https://sling.apache.org/documentation/bundles/models.html#custom-annotations

Regards,

Peter