Expand my Community achievements bar.

SOLVED

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

Avatar

Level 6

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.

1 Accepted Solution

Avatar

Correct answer by
Level 4

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.

View solution in original post

9 Replies

Avatar

Correct answer by
Level 4

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.

Avatar

Level 6

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.

Avatar

Level 4

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

Avatar

Employee

For WcmMode you need the request.

Avatar

Level 6

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.

Avatar

Community Advisor

Hi,

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

more info Apache Sling :: Sling Models



Arun Patidar

Avatar

Level 1

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.

Avatar

Community Advisor

I meant both does the same job.

@SlingObject @Source("sling-object")


Arun Patidar