Hi all,
I'm working on an AEM component and trying to get a property from the dialog using Sling Models. Here's a snippet of my code:
@Model(adaptables = Resource.class)
public class MyComponentModel {
@inject
private String title;
public String getTitle() {
return title;
}
}
However, when I try to use this model in my HTL file like this:
<div data-sly-use.model="com.myproject.models.MyComponentModel">
${model.title}
</div>
The title is always showing as null, even though I have set it in the dialog. Can anyone help me understand what’s going wrong?
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @PriyankaKh4,
You're adapting from Resource
, which works only if the HTL script is executed in a context where resource
is available and points to the correct component node.
However, if you're using the model with a data-sly-use
, AEM might be adapting it from the SlingHttpServletRequest
instead. So you should change the @Model
annotation to adapt from SlingHttpServletRequest
:
@Model(adaptables = SlingHttpServletRequest.class)
public class MyComponentModel {
@Inject
@Optional
private String title;
public String getTitle() {
return title;
}
}
Also, make sure your dialog stores the title
property under the component node.
Sling Models Basics: https://sling.apache.org/documentation/bundles/models.html
Hope that helps!
Hi @PriyankaKh4,
You're adapting from Resource
, which works only if the HTL script is executed in a context where resource
is available and points to the correct component node.
However, if you're using the model with a data-sly-use
, AEM might be adapting it from the SlingHttpServletRequest
instead. So you should change the @Model
annotation to adapt from SlingHttpServletRequest
:
@Model(adaptables = SlingHttpServletRequest.class)
public class MyComponentModel {
@Inject
@Optional
private String title;
public String getTitle() {
return title;
}
}
Also, make sure your dialog stores the title
property under the component node.
Sling Models Basics: https://sling.apache.org/documentation/bundles/models.html
Hope that helps!
Views
Likes
Replies