Expand my Community achievements bar.

SOLVED

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

Avatar

Level 8

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.

1 Accepted Solution

Avatar

Correct answer by
Level 10
8 Replies

Avatar

Level 5

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;

Avatar

Level 8

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.

Avatar

Correct answer by
Level 10

Avatar

Level 8

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

Avatar

Level 10

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

Avatar

Level 10

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?

Avatar

Level 8

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

Avatar

Level 2

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