Expand my Community achievements bar.

SOLVED

Sling Model Injections for children nodes

Avatar

Level 2

Hi need to understand how to inject child node in the sling models and can we inject sling models to other sling models?

Like if we have node hierarchy as below how we can get data using java POJO mapping from sling models by adopting Address node.

Help me out with some pseudo code/reference to adopt children nodes to adopt in sling models.

-> Address

      ->person1

              -> addressdetails[address,city,state,country]

       ->person2

                 -> addressdetails[address,city,state,country]

        ........

        ......

1 Accepted Solution

Avatar

Correct answer by
Level 3

Hi Manjunath,

There is a support collection since Sling Models Impl 1.0.6. But with the default installation in AEM 6.0 you will get 1.0.0 version. For your example you can inject list of address in the PostConstruct method. I have attached a sample code and you can directlydownload and install the code using mvn clean install -PautoInstallPackage and access the servlet in this path http://localhost:4502/bin/testslingmodeluser. Please make sure that you are using Sling Models Impl >= 1.0.6.

Explanation for the provided reference code:

The attached Example has a node

         user1 (parent node) -> name (child node)
                                            -> firstName (property)
                                            -> lastName (property)
                                      -> address (child node)
                                            -> zipid (property)
                                            -> city (property)
                                            -> country (property)
                    
            So now i have a resource user1 and this can be adapted to User.class like:
                        
                        Resource user1 = resourceResolver.getResource("/somepath/for/user");
                        User userData = user1.adaptTo(User.class);
                        
                        Now i can directly access userData.getAddress().getCity(); ->This works because child resources are getting adapted in PostConstruct method.

Attachment URL: https://drive.google.com/file/d/0B1u0tpk84zK1SVZ3WEsxU2lXY1k/view?usp=sharing

View solution in original post

2 Replies

Avatar

Correct answer by
Level 3

Hi Manjunath,

There is a support collection since Sling Models Impl 1.0.6. But with the default installation in AEM 6.0 you will get 1.0.0 version. For your example you can inject list of address in the PostConstruct method. I have attached a sample code and you can directlydownload and install the code using mvn clean install -PautoInstallPackage and access the servlet in this path http://localhost:4502/bin/testslingmodeluser. Please make sure that you are using Sling Models Impl >= 1.0.6.

Explanation for the provided reference code:

The attached Example has a node

         user1 (parent node) -> name (child node)
                                            -> firstName (property)
                                            -> lastName (property)
                                      -> address (child node)
                                            -> zipid (property)
                                            -> city (property)
                                            -> country (property)
                    
            So now i have a resource user1 and this can be adapted to User.class like:
                        
                        Resource user1 = resourceResolver.getResource("/somepath/for/user");
                        User userData = user1.adaptTo(User.class);
                        
                        Now i can directly access userData.getAddress().getCity(); ->This works because child resources are getting adapted in PostConstruct method.

Attachment URL: https://drive.google.com/file/d/0B1u0tpk84zK1SVZ3WEsxU2lXY1k/view?usp=sharing

Avatar

Level 2

Thanks alot kalyan for your response on short time and code sample is good example . I guess it would be help to many people. :) 

@PostConstruct method will have logic to inject to main model. like here in our example list of users user1, user2 ...etc