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.
Solved! Go to Solution.
Views
Replies
Total Likes
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.
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.
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.
Views
Replies
Total Likes
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.
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
Views
Replies
Total Likes
For WcmMode you need the request.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
Following is the method to get resource Resolver without SlingHttpSevletRequest.
Views
Replies
Total Likes
Hi,
Just to add, Also Resource
s can be injected via @SlingObject
more info Apache Sling :: Sling Models
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
I meant both does the same job.
@SlingObject | @Source("sling-object") |
Views
Replies
Total Likes