Expand my Community achievements bar.

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

Avatar

Level 2

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();
}

 

8 Replies

Avatar

Community Advisor

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

Avatar

Level 2

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.

Avatar

Community Advisor

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

Avatar

Community Advisor

@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

Avatar

Level 2

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

Avatar

Administrator

@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