I am learning to use one of the new features of AEM6 - Sling Models. I have already fetched the properties of a node following the steps described here
@Model(adaptables = Resource.class)
public class UserInfo {
@Inject @Named("jcr:title")
private String title;
@Inject @Default(values = "xyz")
private String firstName;
@Inject @Default(values = "xyz")
private String lastName;
@Inject @Default(values = "xyz")
private String city;
@Inject @Default(values = "aem")
private String technology;
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getTechnology() {
return technology;
}
public String getTitle() {
return title;
}
}
and adapted it from a resource
UserInfo userInfo = resource.adaptTo(UserInfo.class);
Now i have the hierarchy as -
+ UserInfo (firstName, lastName, technology)
|
+ UserAddress (houseNo, locality, city, state)
Now I want to fetch the properties of the UserAddress
.
I had got some hints from the documentation page, such as -
If the injected object does not match the desired type and the object implements the Adaptable interface, Sling Models will try to adapt it. This provides the ability to create rich object graphs. For example:
@Model(adaptables = Resource.class)
public interface MyModel {
@Inject
ImageModel getImage();
}
@Model(adaptables = Resource.class)
public interface ImageModel {
@Inject
String getPath();
}
When a resource is adapted to
MyModel
, a child resource named image is automatically adapted to an instance ofImageModel
.
but I don't know how to implement it in my own classes. Please help me out with this.