1
votes

I am using a very basic Sling Model class which is not working at all. This is the class:

package com.aem.sites.models.test;

import org.apache.sling.models.annotations.Model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;

import org.apache.sling.api.resource.Resource;

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

    final static Logger logger = LoggerFactory.getLogger(TestModel.class);

    private String email;

    @PostConstruct
    public void init() {
        logger.info("=====================================================================inside init method");
        email = "[email protected]";
    }

    public String getEmail() {
        return email;
    }
}

I have also included the package in the section like this:

enter image description here

I also looked for the Sling Model class here http://localhost:4502/system/console/status-adapters

and found it's entry like this:

Adaptable: org.apache.sling.api.resource.Resource
Providing Bundle: org.apache.sling.models.impl
Available Adapters:
 * com.aem.sites.models.test.TestModel

What's more surprising to me is the Sling Model class in the package com.aem.sites.models.header is being called properly.

I don't know what's wrong.

Thanks in advance

Sharing the HTL class:

<sly data-sly-use.bannerObj=com.aem.sites.models.test.TestModel">
<section id="banner"
    style="background-image: url('/content/dam/aem-site/banner.jpg')">
    <div class="inner">
        <h2>Today's temperature is</h2>
        <p>
             ${bannerObj.email}
        </p>
        <ul class="actions">
            <li><a href="#content" class="button big special">Sign Up</a></li>
            <li><a href="#elements" class="button big alt">Learn More</a></li>
        </ul>
    </div>
</section>
</sly>

By not working I mean nothing is happening. No errors or any logs are appearing in the error.log file.

2
What do you mean not working? Where are you using the sling model? Can you share the html?Ahmed Musallam
Updated my post with the required information.user972418
Could the class loaded in AEM be an old version? try completely uninstalling the package, removing the package and then making sure the bundle was removed and the model does not show in status-adapters. Then redeploy your project. Also try only com.aem.sites in the sling-model-packages. Just to eliminate any class loader issues.Ahmed Musallam

2 Answers

0
votes

The only issue I see is a syntax error, the data-sly-use attribute's value is not enclosed in quotes properly.

<sly data-sly-use.bannerObj="com.aem.sites.models.test.TestModel">
<section id="banner"
    style="background-image: url('/content/dam/aem-site/banner.jpg')">
    <div class="inner">
        <h2>Today's temperature is</h2>
        <p>
             ${bannerObj.email}
        </p>
        <ul class="actions">
            <li><a href="#content" class="button big special">Sign Up</a></li>
            <li><a href="#elements" class="button big alt">Learn More</a></li>
        </ul>
    </div>
</section>
</sly>

Due to which the HTL file might not have compiled and would have output the entire HTL as is without compiling.

0
votes

You can check from http://localhost:4502/system/console/status-slingmodels which Sling models are available in your instance and to what resources they are bound.

I would make sure my models are listed and then check for other kind of errors, like typos as mentioned in the comment above.