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

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;

  @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

0 Replies

Avatar

Correct answer by
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;
}