Not able to make use of SlingHttpServletRequest in Sling Model in AEM 6.3 | Community
Skip to main content
cquser1
July 16, 2017
Solved

Not able to make use of SlingHttpServletRequest in Sling Model in AEM 6.3

  • July 16, 2017
  • 7 replies
  • 3617 views

Hi,

Currently have a model class with @Model(adaptables = Resource.class). The code written so far works absolutely fine.

To this , I have now added the below

@Model(adaptables = { Resource.class, SlingHttpServletRequest.class })

   @Inject

    SlingHttpServletRequest request;

as I want to make use of request Object in my code.

Referred multiple links on this, Few examples :

https://www.slideshare.net/AEMHub2014/sling-models-by-justin-edelson-final

http://www.wemblog.com/2014/11/how-to-use-sling-models-in-cq56.html

https://forums.adobe.com/thread/2329143

But still not working. " Identifier xysdkfjk cannot be correctly instantiated by the Use API" is the error I keep getting.

Any thoughts on this will be helpful.

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 smacdonald2008

Check Feike code example here -- htl-examples/TitleImpl.java at master · heervisscher/htl-examples · GitHub

7 replies

zeeshanKhan0786
July 16, 2017

You can achieve sling request object by different ways :

@Self

private SlingHttpServletRequest slingHttpServletRequest;

By Constructor

@Model(adaptables= SlingHttpServletRequest.class)

public class TestModel {

  private final SlingHttpServletRequest request;

  public TestModel(SlingHttpServletRequest r){

          this.request = r;

}

By Name

private SlingHttpServletRequest request;

cquser1
cquser1Author
July 16, 2017

Hi zeeshank15007365,

Thank you for your reply.

To get the request Object to my existing class which is working fine, I did the below :

1] Added request to adaptables which just had resource earlier : @Model(adaptables = { Resource.class, SlingHttpServletRequest.class })

2] Tried with

@Self

private SlingHttpServletRequest slingHttpServletRequest;

and

By Name

private SlingHttpServletRequest request;

but no luck. Not sure what is it that I am missing.

Will try the constructor method too and let you know how it goes. Thanks again for replying on this query of mine.

smacdonald2008
smacdonald2008Accepted solution
July 16, 2017
cquser1
cquser1Author
July 16, 2017

The snippet shared by Feike doesn't help me with the use-case I have.

smacdonald2008
July 16, 2017

Feike Visser​ - can you look here when you can - thxs

smacdonald2008
July 16, 2017

Where you able to succesfuuly compile this code? I have never tried to make use of a Request object in a Sling Model. Only a Sling Servlet. What are you trying to do?

cquser1
cquser1Author
July 17, 2017

Hi Scott/Feike,

In component model class, was trying to get request object to use for api resolver.map(). However, now using resolver.map that takes in a single argument rather than the one along with request.

However, it would be helpful if you can let us know how can we take request object in Component model class[in case we need it sometime in the future.]

August 26, 2024

Hey was this resolved , as i am facing the same issue