Skip to main content
New Participant
November 4, 2017
Solved

How to get wcmmode from the class using @Model(adaptables=Resource.class in AEM 6.3

  • November 4, 2017
  • 9 replies
  • 8629 views

In AEM 63, I created a class using sling model adaptables to Resource.class instead of  SlingHttpServletRequest.class because I need to use this method to get MyComponent myComponent = resourceResolver.getResource(myComponentPath), it doesn't work if adaptables=SlingHttpServletRequest.

My question is how to get wcmmode from this MyComponent.class? or get the request from this class?

Thanks.

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 Mallik-Vamaraju

You will always be able to get the handle of SlingHttpServletRequest object in your model(sling models) by injecting it. Here is the code below.

@Inject

SlingHttpServletRequest request;

And once you have the request object, it should be straight forward to get the WCMMode.

WCMMode mode = WCMMode.fromRequest(request);

Let me know if this helps.

9 replies

arunpatidar
New Participant
September 3, 2018

I meant both does the same job.

@SlingObject @Source("sling-object")
Arun Patidar
New Participant
September 3, 2018

Yes Arun, But if we add Resource and SlingHttptServletRequest both in adaptables then we can't inject any other dialog field, only resourceResolver will be injected.

arunpatidar
New Participant
September 3, 2018

Hi,

Just to add, Also Resources can be injected via @SlingObject

more info Apache Sling :: Sling Models

Arun Patidar
New Participant
September 3, 2018

Following is the method to get resource Resolver without SlingHttpSevletRequest.

Getting Resource Resolver in Sling Model. (without using SlingHttpServletRequest) – AEM Queries & Solutions

BigT168Author
New Participant
November 9, 2017

When I tested it, it doesn't work. It complaints the object can't be adaptable. I will try again when I have time and let you know.

Feike_Visser1
Employee
November 5, 2017

For WcmMode you need the request.

New Participant
November 5, 2017

Are you seeing this behavior when you are injecting the request object? was it all working otherwise?  one of the prerequisites is to make these changes(add dependencies) to your pom.xml and also add the plugin element.

  • org.apache.felix.scr
  • org.apache.felix.scr.annotations
  • org.apache.sling.models.api

please refer to this working example just to make sure you are not missing out on any basic steps.

Adobe Experience Manager Help | Creating Adobe Experience Manager 6.3 Sling Model Components

BigT168Author
New Participant
November 5, 2017

Once I inject the code, I get the following error.

org.apache.sling.models.impl.ModelAdapterFactory Could not adapt to model

org.apache.sling.models.factory.MissingElementsException: Could not inject all required fields into class com.capgroup.wcm.content.domain.Article

Any idea what's wrong?

Thanks.

Mallik-VamarajuAccepted solution
New Participant
November 5, 2017

You will always be able to get the handle of SlingHttpServletRequest object in your model(sling models) by injecting it. Here is the code below.

@Inject

SlingHttpServletRequest request;

And once you have the request object, it should be straight forward to get the WCMMode.

WCMMode mode = WCMMode.fromRequest(request);

Let me know if this helps.