Issue with component and sling model | Community
Skip to main content
Level 2
April 22, 2025
Solved

Issue with component and sling model

  • April 22, 2025
  • 1 reply
  • 333 views

I am working on a component that uses a Sling Model and HTL, but the CTA link is not rendering on the page. Below is the code

Sling Model

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class BannerModel { @ValueMapValue private String title; @ValueMapValue private String ctaText; @ValueMapValue private String ctaLink; public String getTitle() { return title; } public String getCtaText() { return ctaText; } public String getCtaLink() { return ctaLink; } }

 banner.html

<div data-sly-use.model="com.techinnovia.core.models.BannerModel"> <h1>${model.title}</h1> <a href="${model.ctaLink}">${model.ctaText}</a> </div>

The component renders title and ctaText fine, but ctaLink always shows as null. No errors in logs.
What could be wrong here?

Best answer by SantoshSai

Hi @luca_moreau,

There might be few things you could double check:

  • Check if your HTL is expecting the model to adapt from a SlingHttpServletRequest, and may be your model uses Resource, in this case it won't adapt correctly.
  • Check if dialog field is saving properly - make sure the dialog field uses name="./ctaLink"
  • If name mismatch in between JCR and Java - in this case it won't be injected automatically.
    eg. If it’s saved as cta_link, but your model field is ctaLink
    @ValueMapValue(name = "cta_link") private String ctaLink;

hope this helps!

1 reply

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
April 22, 2025

Hi @luca_moreau,

There might be few things you could double check:

  • Check if your HTL is expecting the model to adapt from a SlingHttpServletRequest, and may be your model uses Resource, in this case it won't adapt correctly.
  • Check if dialog field is saving properly - make sure the dialog field uses name="./ctaLink"
  • If name mismatch in between JCR and Java - in this case it won't be injected automatically.
    eg. If it’s saved as cta_link, but your model field is ctaLink
    @ValueMapValue(name = "cta_link") private String ctaLink;

hope this helps!

Santosh Sai