Skip to main content
cquser1
Level 7
Jul 16, 2017 7:29 AM UTC
Solved

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

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.
smacdonald2008
smacdonald2008Accepted solution
Level 10
Jul 16, 2017 3:13 PM UTC

8 replies

zeeshanKhan0786
Level 5
Jul 16, 2017 1:30 PM UTC

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;

Translate
cquser1
cquser1Author
Level 7
Jul 16, 2017 1:59 PM UTC

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.

Translate
smacdonald2008
smacdonald2008Accepted solution
Level 10
Jul 16, 2017 3:13 PM UTC
cquser1
cquser1Author
Level 7
Jul 16, 2017 4:06 PM UTC

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

Translate
smacdonald2008
Level 10
Jul 16, 2017 5:03 PM UTC

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

Translate
smacdonald2008
Level 10
Jul 16, 2017 5:04 PM UTC

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?

Translate
cquser1
cquser1Author
Level 7
Jul 17, 2017 1:36 PM UTC

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.]

Translate
Level 2
Aug 26, 2024 12:01 PM UTC

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

Translate