Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list
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;
}

 

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;
}