SlingHttpServletRequest request value is null while Injecting SlingHttpServletRequest to the sling model adaptable to Resource and SlingHttpServletRequest. | Community
Skip to main content
Level 2
May 11, 2018 10:08 AM UTC

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

@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;

}

}

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

7 replies

New Participant
Apr 25, 2020 7:51 AM UTC

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

Translate
manasipAuthor
Level 2
May 11, 2018 4:17 PM UTC

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?

Translate
sharathe9008234
New Participant
May 15, 2019 8:46 PM UTC

Hi,
Is this issue resolved ?

I am also facing similar issue while exporting columnCotrol component.

Thanks!

Sharath

Translate
navinkaushal
Level 4
May 11, 2018 3:27 PM UTC
smacdonald2008
Level 10
May 11, 2018 1:19 PM UTC

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

Translate
Vijayalakshmi_S
Level 10
May 11, 2018 1:17 PM UTC

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

Translate