Unable to get / set data from sling models

We have a sling model. for example

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

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

      

We also added

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

      

After that, we can open the edit dialog for that component and check that some data has been inserted. We can see this data in the JCR But when we try to get the content of the 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 advice? How can I debug Sling?

Can anyone add the "Sightly" tag to this post?

+3


source to share


3 answers


Perhaps your package is importing the tag javax.inject.Inject

from the package org.apache.sling.scripting.java

as opposed to the Sling Models package. In AEM6 org.apache.sling.scripting.java

, the Sling Models package and kit expose this package, and if your kit ends up getting an import from the old one, Sling Models will not recognize your import annotations.

I managed to get around this in my example by adding <Require-Bundle>org.apache.sling.models.api</Require-Bundle>

the maven-bundle-plugin to the config, essentially adding a Require-Bundle directive to force my bundle to use the bundle org.apache.sling.models.api

.



To find this, I downloaded the Sling source code from the Sling SVN repository, opened it in the IDE and attached a debugger to my current AEM instance, and set breakpoints in the Sling Models bundle to see how the mechanism tries to resolve import annotations.

+6


source


I am trying to do the same with a servlet, at first it showed me null, after that it works great, but I don't know what you are missing. For your reference, I made a git repository for this, https://github.com/gargshivani111/slingmodels



Hope this helps you.

+7


source


Had the same problem, but in my case the models package was not installed in the <Export-Package> tag.

If the Export-Package tag is used in the pom, maven-bundle-plugin, the sling models package must be added to it.

+1


source







All Articles