Initialize a model class in a different model class | Community
Skip to main content
New Participant
March 6, 2023
Solved

Initialize a model class in a different model class

  • March 6, 2023
  • 3 replies
  • 2816 views

Hi Community,

 

I have a query, please help me with this question.

 

I am trying to call a get method in a model class which is inside another model class, I will give a simple example below.

I hope the below example is sufficient enough to answer my Question, Thank you for the replies.

 

Class HelloWorldModelImpl:

 

@Model(adaptables = {Resource.class, SlingHttpServletRequest.class}, adapters= HelloWorldModel.class, 
resourceType = HelloWorldModelImpl.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
public class HelloWorldModelImpl {

//
private EmployeeModel employeeModel;

public String getEmpID(){
return employeeModel.getEmpID();
}

public String getEmpName(){
return employeeModel.getEmpName();
}

}

 

Class EmployeeModel:

@Model(adaptables = Resource.class,defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class EmployeeModel{

@ValueMapValue
private String empId;

@ValueMaapValue
private String empName;

public String getEmpID(){
return empId;
}

public String getEmpName(){
return empName;
}
}

 

Thanks

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

@vinod-n-e calling ModelB class methods from ModelA, the below code works for me, please try: 

Note: Just add @Self as well

Once you've modelB object you can call methods.

@Model(adaptables = Resource.class,defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL )
public class ModelA { 
@Inject
@Self
private ModelB modelB;

}

 

3 replies

krishna_sai
New Participant
March 7, 2023

@vinod-n-e you can also try adapting resource to ModelB

@SlingObject Resource resource ModelB modelB = resource.adaptTo(ModelB.class)


Hope this helps,
Krishna

Vinod-N-EAuthor
New Participant
March 7, 2023

Thanks Krishna I will check this as well some other time.

Siva_Sogalapalli
Siva_SogalapalliAccepted solution
New Participant
March 6, 2023

@vinod-n-e calling ModelB class methods from ModelA, the below code works for me, please try: 

Note: Just add @Self as well

Once you've modelB object you can call methods.

@Model(adaptables = Resource.class,defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL )
public class ModelA { 
@Inject
@Self
private ModelB modelB;

}

 

Vinod-N-EAuthor
New Participant
March 7, 2023

Thank you Siva for helping me out.

arunpatidar
New Participant
March 6, 2023

I think it is straight forward,

you can inject a child resource or adapt the current resource to another model

Available injectors  : https://sling.apache.org/documentation/bundles/models.html#available-injectors-1 

 

 

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class OneModel { @Self Resource resource; @Inject private LinkModel link;

 

 

Arun Patidar
Vinod-N-EAuthor
New Participant
March 6, 2023

Sorry Arun, Your answer is not satisfying. 

 

As, I am trying to understand if I could achieve something like the example I have given. As I tried something like the example I have given, the EmployeeModel class is null while accessing that model class.

arunpatidar
New Participant
March 6, 2023

In Sling Model, It is possible to call one sling Model from another.

I am not sure about your content structure so can't really help with the actual code but just from concept size its is possible to inject another custom type(sling model) in sling model

Arun Patidar