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
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

How to inject a sling model inside another sling model?

Vanitha
Level 1
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
piotrr32520682
Correct answer by
Level 2
Level 2

Try this:

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

  private String title;

  @Self
  ModelB modelb;
}

 

Or if ModelB is adaptable only from Resource then

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

   private String title;

   @Self
   @Via("resource")
   ModelB modelb;
}

 

View solution in original post

1 Reply
piotrr32520682
Correct answer by
Level 2
Level 2

Try this:

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

  private String title;

  @Self
  ModelB modelb;
}

 

Or if ModelB is adaptable only from Resource then

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

   private String title;

   @Self
   @Via("resource")
   ModelB modelb;
}

 

View solution in original post