cq5 - How to adapt a child node in sling model of aem6 -


i learning use 1 of new features of aem6 - sling models. have fetched properties of node following 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 resource

userinfo userinfo = resource.adaptto(userinfo.class); 

now have hierarchy -

+ userinfo (firstname, lastname, technology)   |   + useraddress (houseno, locality, city, state) 

now want fetch properties of useraddress.

i had got hints documentation page, such -

if injected object not match desired type , object implements adaptable interface, sling models try adapt it. provides ability create rich object graphs. example:

@model(adaptables = resource.class) public interface mymodel {    @inject   imagemodel getimage(); }  @model(adaptables = resource.class) public interface imagemodel {    @inject   string getpath(); } 

when resource adapted mymodel, child resource named image automatically adapted instance of imagemodel.

but don't know how implement in own classes. please me out this.

it sounds need separate class useraddress wrap houseno, city, state , locality properties.

+ userinfo (firstname, lastname, technology)   |   + useraddress (houseno, locality, city, state) 

just mirror structure outlined in sling models.

create useraddress model:

@model(adaptables = resource.class) public class useraddress {      @inject     private string houseno;      @inject     private string locality;      @inject     private string city;      @inject     private string state;      //getters } 

this model can used in userinfo class:

@model(adaptables = resource.class) public class userinfo {      /*      * assumes hierarchy described       * mirrored in content structure.      * resource you're adapting userinfo      * expected have child resource named      * useraddress. @named annotation should      * work here if need reason.      */     @inject     @optional     private useraddress useraddress;      public useraddress getuseraddress() {         return this.useraddress;     }      //simple properties (strings , built-in types) omitted brevity } 

you can tweak behaviour additional annotations default values , optional fields general idea.

in general, sling models should able handle injection of model long finds appropriate adaptable. in case, it's sling model i've done legacy classes based on adapter factories well.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -