Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to inject a sling model inside another sling model?

Avatar

Level 1

I tried as below,

 

Model A:

@Model(adaptables = SlingHttpServletRequest.class)

public class ModelA {

  private String title;

  @inject
   Resource resource;
  ModelB modelb;

.    @PostConstruct

      public void init() {

     modelb = resource.adaptTo(ModelB.class);

      }

}



Model B:

@Model(adaptables = SlingHttpServletRequest.class)

public class ModelB {

   private String text;

}

 

But it's not working.

1 Accepted Solution

Avatar

Correct answer by
Level 2

Try this:

@Model(adaptables = SlingHttpServletRequest.class)
public class ModelA {

  private String title;

  @Deleted Account
  ModelB modelb;
}

 

Or if ModelB is adaptable only from Resource then

@Model(adaptables = SlingHttpServletRequest.class)
public class ModelA {

   private String title;

   @Deleted Account
   @Via("resource")
   ModelB modelb;
}

 

View solution in original post

2 Replies

Avatar

Correct answer by
Level 2

Try this:

@Model(adaptables = SlingHttpServletRequest.class)
public class ModelA {

  private String title;

  @Deleted Account
  ModelB modelb;
}

 

Or if ModelB is adaptable only from Resource then

@Model(adaptables = SlingHttpServletRequest.class)
public class ModelA {

   private String title;

   @Deleted Account
   @Via("resource")
   ModelB modelb;
}