Sling Models request object | Community
Skip to main content
Prince_Shivhare
New Participant
Apr 30, 2021 5:39 PM UTC
Solved

Sling Models request object

Hi All,

 

I want to use the SlingHttpServletRequest request object in another sling model.

 

For ex.-

 

@Getter
@Model(adaptables = {Resource.class,SlingHttpServletRequest.class}, adapters = Header.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class Header{
@SlingObject
Resource componentResource;
@SlingObject
SlingHttpServletRequest servletrequest;
@ValueMapValue
private String fileLarge;
@ChildResource(name = "b2cnavigationItem")
private List<navigation> b2cnavigationItem;
}


@Model(adaptables = {Resource.class,SlingHttpServletRequest.class}, adapters = navigation.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class navigation{
@SlingObject
Resource resource;
@OSGiService
SlingHttpServletRequest request;
@ValueMapValue
String domain;
@Getter
@ValueMapValue
private String navigationText;
@Getter
@ValueMapValue
private String navigationLink;
@ValueMapValue
private String expFragPath;
}

Above is the code, Now  I want to use the request object in "navigation" class. but I am getting Null.

However, I am getting request object in Header Class. but not getting in navigation class.

 

Can anyone help me here? I want to append the Request Url host in the expFragPath variable.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
BrianKasingli
BrianKasingliAccepted solution
New Participant
May 3, 2021 12:27 PM UTC

Try removing adaptors:

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

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

Also sometimes Sling Model injector's annotations can be confusing. During development, if you have this guide opened, it will allow you to accurately write/use annotations without any confusion - https://sourcedcode.com/blog/aem/aem-sling-model-injectors-annotations-reference-guide

 

8 replies

Vijayalakshmi_S
Level 10
May 4, 2021 1:45 AM UTC

Hi @prince_shivhare,

When injecting a resource as @ChildResource(In this case, Navigation), it loses the context of SlingHttpServletRequest and hence the same is null in resulting/its respective Model.

To overcome this, we have annotation named @ChildResourceFromRequest from ACS commons - https://adobe-consulting-services.github.io/acs-aem-commons/features/sling-model-injectors/child-resource-from-request/index.html

 

Use @ChildResourceFromRequest (com.adobe.acs.commons.models.injectors.annotation.ChildResourceFromRequest) instead of @ChildResource in Header Model + SlingHttpServletRequest in Navigation Model using @SlingObject or using @Self. 

 

ACS commons maven dependency details are available here - https://adobe-consulting-services.github.io/acs-aem-commons/pages/maven.html

Translate
Prince_Shivhare
New Participant
May 4, 2021 6:05 AM UTC
Thanks but tried that too and it is not working. however I have resolved my issue using the JS.
Translate
Level 3
Nov 4, 2022 2:33 PM UTC

@prince_shivhare  Replace @OSGIService annotation in navigation with @SlingObject, remove adapter in both models, use only SlingHttpServletRequest.class as adaptable in both models, it should resolve your issue. 

Translate
BrianKasingli
BrianKasingliAccepted solution
New Participant
May 3, 2021 12:27 PM UTC

Try removing adaptors:

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

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

Also sometimes Sling Model injector's annotations can be confusing. During development, if you have this guide opened, it will allow you to accurately write/use annotations without any confusion - https://sourcedcode.com/blog/aem/aem-sling-model-injectors-annotations-reference-guide

 

Translate
Prince_Shivhare
New Participant
May 4, 2021 11:37 AM UTC
Not working Mate, I resolved my issue with different approach.
Translate
Asutosh_Jena_
New Participant
Apr 30, 2021 5:59 PM UTC

Hi @prince_shivhare 

 

I see in Navigation model you are using @Reference whereas you should be using it as @SlingObject. Also you need to remove the adapters which is not required.

@SlingObject
SlingHttpServletRequest request;

This should resolve the issue.

 

Thanks! 

Translate
Prince_Shivhare
New Participant
Apr 30, 2021 6:34 PM UTC
Nope, that is also not working.
Translate