Search code examples
aemslinghtl

AEM 6.3 - Sling Model not working


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.


Solution

  • 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.