SlingHttpServletRequest request value is null while Injecting SlingHttpServletRequest to the sling model adaptable to Resource and SlingHttpServletRequest.

Avatar

Avatar
Validate 1
Level 2
manasip
Level 2

Likes

3 likes

Total Posts

12 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
View profile

Avatar
Validate 1
Level 2
manasip
Level 2

Likes

3 likes

Total Posts

12 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
View profile
manasip
Level 2

11-05-2018

@Model(adaptables = {Resource.class, SlingHttpServletRequest.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL, resourceType = "reactaem/components/content/my-title")

@Exporter(name = "jackson", extensions = "json")

public class MyTitleModel {

private final Logger logger = LoggerFactory.getLogger(getClass());

@Inject

private String title;

@Inject

private String text;

@Inject

SlingHttpServletRequest slingRequest;

@PostConstruct

    public void init() {

if (slingRequest != null) {

String url = slingRequest.getRequestPathInfo().getResourcePath() ;

logger.info("URL=" + url);

}

else {

logger.info("SlingHttpServletRequest is null*****");

}

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getText() {

return text;

}

public void setText(String text) {

this.text = text;

}

}

Replies

Avatar

Avatar
Boost 500
MVP
Vijayalakshmi_S
MVP

Likes

573 likes

Total Posts

728 posts

Correct reply

240 solutions
Top badges earned
Boost 500
Give Back 50
Give Back 5
Ignite 10
Ignite 5
View profile

Avatar
Boost 500
MVP
Vijayalakshmi_S
MVP

Likes

573 likes

Total Posts

728 posts

Correct reply

240 solutions
Top badges earned
Boost 500
Give Back 50
Give Back 5
Ignite 10
Ignite 5
View profile
Vijayalakshmi_S
MVP

11-05-2018

Hi,

When the adaptable itself needs to be injected, we need to use an annotation named "@Self" along with "@Inject".

In this case, add self annotation to SlingHttpServletRequest.

See if it works with this.

Reference:

Apache Sling :: Sling Models

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

11-05-2018

See this article - it will point you in the right direction -- Scott's Digital Community: Performing Sling Model Adaptation Using Request and Resource Objects

Avatar

Avatar
Give Back 5
Level 3
navinkaushal
Level 3

Likes

16 likes

Total Posts

70 posts

Correct reply

7 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back 25
Give Back 10
Give Back
View profile

Avatar
Give Back 5
Level 3
navinkaushal
Level 3

Likes

16 likes

Total Posts

70 posts

Correct reply

7 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back 25
Give Back 10
Give Back
View profile
navinkaushal
Level 3

11-05-2018

Avatar

Avatar
Validate 1
Level 2
manasip
Level 2

Likes

3 likes

Total Posts

12 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
View profile

Avatar
Validate 1
Level 2
manasip
Level 2

Likes

3 likes

Total Posts

12 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
View profile
manasip
Level 2

11-05-2018

I am developing on AEM 6.3

I have already tried:

@Self

@Inject

private SlingHttpServletRequest slingRequest;

and

@SlingObject

private SlingHttpServletRequest slingRequest;

Any other way to get the SlingHttpServletRequest object?

Avatar

Avatar
Level 1
sharathe9008234
Level 1

Likes

0 likes

Total Posts

1 post

Correct reply

0 solutions
View profile

Avatar
Level 1
sharathe9008234
Level 1

Likes

0 likes

Total Posts

1 post

Correct reply

0 solutions
View profile
sharathe9008234
Level 1

15-05-2019

Hi,
Is this issue resolved ?

I am also facing similar issue while exporting columnCotrol component.

Thanks!

Sharath

Avatar

Avatar
Give Back
Level 1
rajesh0926
Level 1

Likes

0 likes

Total Posts

1 post

Correct reply

0 solutions
Top badges earned
Give Back
View profile

Avatar
Give Back
Level 1
rajesh0926
Level 1

Likes

0 likes

Total Posts

1 post

Correct reply

0 solutions
Top badges earned
Give Back
View profile
rajesh0926
Level 1

30-03-2020

Seems to be order of the adaptables does matter here

adaptables = {SlingHttpServletRequest.class, Resource.class }

Having sling request first in the list is solving the issue for me

Avatar

Avatar
Give Back
Level 1
carolinaguedes
Level 1

Likes

0 likes

Total Posts

1 post

Correct reply

0 solutions
Top badges earned
Give Back
View profile

Avatar
Give Back
Level 1
carolinaguedes
Level 1

Likes

0 likes

Total Posts

1 post

Correct reply

0 solutions
Top badges earned
Give Back
View profile
carolinaguedes
Level 1

25-04-2020

hey, having the same problem. Any solution? Thank you