How to get wcmmode from the class using @Model(adaptables=Resource.class in AEM 6.3 | Community
Skip to main content
Level 5
Nov 4, 2017 11:24 PM UTC
Solved

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

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.
Mallik-VamarajuAccepted solution
Level 3
Nov 5, 2017 4:49 AM UTC

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
Sep 3, 2018 9:23 AM UTC

I meant both does the same job.

@SlingObject @Source("sling-object")
Translate
Arun Patidar
New Participant
Sep 3, 2018 9:01 AM UTC

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.

Translate
arunpatidar
New Participant
Sep 3, 2018 7:54 AM UTC

Hi,

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

more info Apache Sling :: Sling Models

Translate
Arun Patidar
New Participant
Sep 3, 2018 6:20 AM UTC

Following is the method to get resource Resolver without SlingHttpSevletRequest.

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

Translate
BigT168Author
Level 5
Nov 9, 2017 5:48 AM UTC

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.

Translate
Feike_Visser1
Employee
Nov 5, 2017 1:22 PM UTC

For WcmMode you need the request.

Translate
Level 3
Nov 5, 2017 7:10 AM UTC

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

Translate
BigT168Author
Level 5
Nov 5, 2017 6:31 AM UTC

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.

Translate
Mallik-VamarajuAccepted solution
Level 3
Nov 5, 2017 4:49 AM UTC

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.

Translate