Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

Sling Models request object

Avatar

Avatar
Springboard
Level 7
Prince_Shivhare
Level 7

Likes

112 likes

Total Posts

394 posts

Correct Reply

64 solutions
Top badges earned
Springboard
Establish
Give Back 100
Boost 100
Validate 1
View profile

Avatar
Springboard
Level 7
Prince_Shivhare
Level 7

Likes

112 likes

Total Posts

394 posts

Correct Reply

64 solutions
Top badges earned
Springboard
Establish
Give Back 100
Boost 100
Validate 1
View profile
Prince_Shivhare
Level 7

30-04-2021

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.

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

613 likes

Total Posts

591 posts

Correct Reply

231 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

613 likes

Total Posts

591 posts

Correct Reply

231 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
BrianKasingli
MVP

03-05-2021

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

 

Answers (2)

Answers (2)

Avatar

Avatar
Boost 500
MVP
Vijayalakshmi_S
MVP

Likes

511 likes

Total Posts

647 posts

Correct Reply

213 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

511 likes

Total Posts

647 posts

Correct Reply

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

03-05-2021

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

 

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

Avatar

Avatar
Give Back 100
Level 10
asutosh_jena
Level 10

Likes

560 likes

Total Posts

677 posts

Correct Reply

194 solutions
Top badges earned
Give Back 100
Boost 500
Affirm 100
Ignite 1
Establish
View profile

Avatar
Give Back 100
Level 10
asutosh_jena
Level 10

Likes

560 likes

Total Posts

677 posts

Correct Reply

194 solutions
Top badges earned
Give Back 100
Boost 500
Affirm 100
Ignite 1
Establish
View profile
asutosh_jena
Level 10

30-04-2021

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!