Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get/set data from sling models

We have sling model. For example

@Model(adaptables=Resource.class)
public class MyModel {

@Inject
private String propertyName;
}
public Image getPropertyName) {
    return propertyName;
}

Also we have added

<Sling-Model-Packages>
  org.apache.sling.models.it.models
</Sling-Model-Packages>

After this we can open edit dialog for this component and check that some data was inserted. We can see this data in JCR But when we are trying to get content of propertyName via Sightly

<div class="feature-wrapper" data-sly-use.model="org.apache.sling.models.it.models.MyModel">
<div data-sly-test="${model.propertyName}" data-sly-unwrap>

model.propertyName will be empty

Any ideas or advices? How I can debug Sling?

Can anyone add tag "Sightly" to this post?

like image 450
MastAvalons Avatar asked Dec 09 '22 06:12

MastAvalons


2 Answers

I try to do the same thing with the help of servlet,firstly it was showing me null,after that it works fine,but i dont know what you are missing.For your reference i made a git repo for this. https://github.com/gargshivani111/slingmodels

Hope it will help you.

like image 126
Shivani Garg Avatar answered Jan 13 '23 09:01

Shivani Garg


It is possible that your bundle is importing the javax.inject.Inject tag from the org.apache.sling.scripting.java bundle as opposed to the Sling Models bundle. In AEM6 the org.apache.sling.scripting.java bundle and the Sling Models bundle expose this package and if your bundle ends up getting the import from the former, Sling Models won't recognize your Import annotations.

I was able to get around this in my instance by adding <Require-Bundle>org.apache.sling.models.api</Require-Bundle> to the maven-bundle-plugin configuration, essentially adding a Require-Bundle directive forcing my bundle to use org.apache.sling.models.api bundle.

To find this, I downloaded the Sling source code from the Sling SVN repository, opened it in an IDE, and attached a debugger to my running AEM instance and set break points within the Sling Models bundle to see how the mechanism was attempting to resolve the import annotations.

like image 21
Paul Michelotti Avatar answered Jan 13 '23 08:01

Paul Michelotti