How to use sling model request object from one model class to another | Community
Skip to main content
Level 2
April 3, 2024

How to use sling model request object from one model class to another

  • April 3, 2024
  • 4 replies
  • 2081 views

Hi i am new to AEM, and stuck with one issue: I want use the request object of DemoModel, into CallToActionModel to call one of the util methods, but this does not seems to work, can anyone please help with this, below is the code snippet

 

@Model(adaptables = { SlingHttpServletRequest.class,
Resource.class }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@Exporter(name =jackson, extensions = json)
public class DemoModel extends WebComponentModel{

@SlingObject
private SlingHttpServletRequest request;

@ChildResource
@Expose
private List<DemoModelList> site;

}
Model(adaptables = { SlingHttpServletRequest.class,
Resource.class }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class DemoModelList extends WebComponentModel {

@ChildResource
@Expose
private List<CallToActionModel> links;

}

 

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

@Inject
private SlingHttpServletRequest request;

@Default(booleanValues = false)
@ValueMapValue
@Expose
@Getter
@Setter
private boolean isSiteCheck;

@Override
@PostConstruct
protected void init() {
isDealerSite = isSiteCheck && AEMUtils.isSite(request);
super.init();
}

 

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

4 replies

Anudeep_Garnepudi
Community Advisor
Community Advisor
April 3, 2024

@c-shjain 

 

If both the components are part of same page, then same request object will flow through entire page. You don't need to pass it from one Model to another.

 

Can you share more details about error if any or what exactly not working.

c-shjainAuthor
Level 2
April 3, 2024

yes they are part of same page but different classes, so what code change is required to achive that, i just want the isSiteCheck to become true when both the conditions are met, but i am getting request object as null.

Anudeep_Garnepudi
Community Advisor
Community Advisor
April 3, 2024

Try @Self instead of @586265 annotation for request object.

arunpatidar
Community Advisor
Community Advisor
April 8, 2024

Hi @c-shjain 

Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Arun Patidar
aanchal-sikka
Community Advisor
Community Advisor
April 14, 2024

@c-shjain 

 

Please use following in CallToActionModel and give it a try:

 

 

@ScriptVariable private SlingHttpServletRequest request;

 

 

You can also refer to https://techrevel.blog/2017/03/18/sling-model-annotations/. It has various code sample on how to get objects in Sling Models

 

1. Refrain from using SlingHttpServletRequest.class, Resource.class together. Details are there in blog

2. Refrain from using @Inject. Details available in blog

Aanchal Sikka
c-shjainAuthor
Level 2
April 17, 2024

Even after changing the annotation to @ScriptVariable, the object is still null.

kautuk_sahni
Community Manager
Community Manager
June 10, 2024

@c-shjain Did you find the suggestions from users helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!

Kautuk Sahni
c-shjainAuthor
Level 2
August 2, 2024

Nothing worked for me so i changed the complete approach and used 2 different classes to achieve this.