Expand my Community achievements bar.

SOLVED

Issue in fetching property

Avatar

Level 2

 

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?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

References:

Hope that helps!


Santosh Sai

AEM BlogsLinkedIn


View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

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.

References:

Hope that helps!


Santosh Sai

AEM BlogsLinkedIn