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

Dynamic injection of values into sling model is not working

Avatar

Avatar
Give Back 5
Level 2
krishc76025392
Level 2

Likes

4 likes

Total Posts

19 posts

Correct Reply

0 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back
Ignite 5
Ignite 3
View profile

Avatar
Give Back 5
Level 2
krishc76025392
Level 2

Likes

4 likes

Total Posts

19 posts

Correct Reply

0 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back
Ignite 5
Ignite 3
View profile
krishc76025392
Level 2

25-04-2019

Hi All,

i am trying to inject some values dynamically into sling model and retrieve some properties of that injected value and print them as below which is not working and throwing a null pointer exception which is not appropriate.

i can see sling model is registered in adapters.

some one please tell me where it went wrong.

Any help is highly appreciated.

In below i want to send each item of list dynamically to sling model via "selectPath" and process the valur for a property in sling model and retrieve in "flags.flagicon"

my sightly html:

<select class="list-selector__dropdown language-change" title="languages" data-sly-list.item="${listNavigation.items}">

            <sly data-sly-use.flags="${'com.krish.chopps.modules.sling.models.listselector.ListSelectorModel' @ selectPath='${item.path}' }">

              <option value="${item.language}">${item.title}, ${flags.flagicon}</option>

        </select>

          </sly>

My Sling model:

@Model(adaptables = { SlingHttpServletRequest.class, Resource.class })

public class ListSelectorModel {

   private static final Logger logger = LoggerFactory.getLogger(ListSelectorModel.class);

   @RequestAttribute
  @Optional
   private String selectPath;

   @SlingObject
   private ResourceResolver resolver;

   public String getFlagicon() {

   return flagicon;

  }

   private String flagicon;

   @PostConstruct
   public void init() {

   if (selectPath != null) {

   flagicon = getNodeValue(selectPath, "countryFlag");

  }

  }

   private String getNodeValue(String parentPage, String property) {

   logger.info("parentpage is "+ parentPage);

  Resource resource = resolver.getResource(parentPage + "/jcr:content");

  Node node = resource.adaptTo(Node.class);

  String image = null;

   try {

   if (node != null && node.hasProperty(property)) {

  image = node.getProperty(property).getString();

  }

  } catch (RepositoryException e) {

   logger.info("Exception in node values : {}", e);

  }

   return image;

  }

}

Replies

Avatar

Avatar
Establish
MVP
Gaurav-Behl
MVP

Likes

248 likes

Total Posts

1,147 posts

Correct Reply

283 solutions
Top badges earned
Establish
Give back 300
Give Back 50
Give Back 5
Give Back 3
View profile

Avatar
Establish
MVP
Gaurav-Behl
MVP

Likes

248 likes

Total Posts

1,147 posts

Correct Reply

283 solutions
Top badges earned
Establish
Give back 300
Give Back 50
Give Back 5
Give Back 3
View profile
Gaurav-Behl
MVP

25-04-2019

What does 'listNavigation' object maps to in the HTL script?

Did you print/debug in model object to validate the image object?

Not sure why you don't prefer to use the 'self' annotation/object to get the data?

Avatar

Avatar
Give Back 5
Level 2
krishc76025392
Level 2

Likes

4 likes

Total Posts

19 posts

Correct Reply

0 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back
Ignite 5
Ignite 3
View profile

Avatar
Give Back 5
Level 2
krishc76025392
Level 2

Likes

4 likes

Total Posts

19 posts

Correct Reply

0 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back
Ignite 5
Ignite 3
View profile
krishc76025392
Level 2

25-04-2019

in HTL i used list navigation as below

<sly data-sly-use.listNavigation="com.adobe.cq.wcm.core.components.models.LanguageNavigation"></sly>

Avatar

Avatar
Establish
MVP
Gaurav-Behl
MVP

Likes

248 likes

Total Posts

1,147 posts

Correct Reply

283 solutions
Top badges earned
Establish
Give back 300
Give Back 50
Give Back 5
Give Back 3
View profile

Avatar
Establish
MVP
Gaurav-Behl
MVP

Likes

248 likes

Total Posts

1,147 posts

Correct Reply

283 solutions
Top badges earned
Establish
Give back 300
Give Back 50
Give Back 5
Give Back 3
View profile
Gaurav-Behl
MVP

25-04-2019

Avatar

Avatar
Level 1
reshmabegum
Level 1

Likes

0 likes

Total Posts

2 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
reshmabegum
Level 1

Likes

0 likes

Total Posts

2 posts

Correct Reply

0 solutions
View profile
reshmabegum
Level 1

25-04-2019

Use @Inject instead of @RequestAttribute

@Model(adaptables = { SlingHttpServletRequest.class, Resource.class })

public class ListSelectorModel {

   private static final Logger logger = LoggerFactory.getLogger(ListSelectorModel.class);

   @Inject
  @Optional
   private String selectPath;

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,408 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,408 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

25-04-2019

We cover how to work with Sling Models and Show HOW TO inject values here:

Building Experience Manager 6.5 Components using Granite/Coral Resource Types

I recommend going through this entire document to learn how to develop AEM Components - including working with Multifield.

Avatar

Avatar
Level 1
reshmabegum
Level 1

Likes

0 likes

Total Posts

2 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
reshmabegum
Level 1

Likes

0 likes

Total Posts

2 posts

Correct Reply

0 solutions
View profile
reshmabegum
Level 1

25-04-2019

Use below syntax

<sly data-sly-use.flags="${'com.krish.chopps.modules.sling.models.listselector.ListSelectorModel' @ selectPath=item.path }">

and

Use @Inject instead of @RequestAttribute

@Model(adaptables = { SlingHttpServletRequest.class, Resource.class })

public class ListSelectorModel {

   private static final Logger logger = LoggerFactory.getLogger(ListSelectorModel.class);

   @Inject
  @Optional
   private String selectPath;