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
Solved! Go to Solution.
Views
Replies
Total Likes
@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;
}
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;
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.
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
@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;
}
Thank you Siva for helping me out.
@Vinod-N-E you can also try adapting resource to ModelB
@SlingObject
Resource resource
ModelB modelB = resource.adaptTo(ModelB.class)
Hope this helps,
Krishna
Thanks Krishna I will check this as well some other time.
Views
Likes
Replies