Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

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

Avatar

Level 5

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 3

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 3

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 5

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 3

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

Level 5

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

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")