Skip to content

HTL Use-API: Binding Java and JavaScript Logic to Templates

The HTL Use-API in AEM: Sling Models, Use POJOs and WCMUsePojo, how each receives bindings and parameters, and where the deprecated JavaScript Use-API stands.

Rewritten in December 2026.

On AEM as a Cloud Service the JavaScript Use-API, server-side scripts that call use(), is deprecated, with the Java Use API named as its replacement. What remains is Java: a Sling Model, a POJO implementing Use, or a subclass of AEM's WCMUsePojo. A data-sly-use attribute names the class, the engine builds it and hands it the script's bindings plus any parameters, and HTL reads its getters. Identifiers, scope and template libraries are in the data-sly-use reference.

What does the deprecation mean on Cloud Service and 6.5?

Adobe's table gives no removal date. Adobe's HTL documentation dropped its JavaScript page too: the old use-api-javascript.html address redirects to the Java Use-API page.

It isn't on the AEM 6.5 deprecated-features list. For Cloud Service, write new logic in Java and port each .js Use object when you next change its component. On 6.5, existing scripts can stay.

How does a JavaScript Use object receive data?

Parameters arrive as properties of this, and the script bindings (properties, resource, request and the rest) are globals. Whatever the function returns is what HTL sees:

// /apps/my-site/components/teaser/short-title.js
"use strict";
use(function () {
    var title = String(properties.get("jcr:title", ""));
    var maxLength = this.maxLength || 60;
    return {
        shortTitle: title.length > maxLength ? title.substring(0, maxLength) + "..." : title
    };
});
<h2 data-sly-use.t="${'short-title.js' @ maxLength=40}">${t.shortTitle}</h2>

The script runs in Rhino, where values coming from Java stay Java objects unless you convert them. There, new java.lang.String("apples") === "apples" evaluates to false. Wrapping the title in String() turns it into a JavaScript string before length or a strict comparison touches it. JavaScript Use objects are also slower to execute than Sling Models and Java Use objects.

The three Java options

ClassHow it gets contextHow it gets parameters
Sling ModelInjection from the request or resource it adapts@RequestAttribute, during creation only
POJO implementing Useinit(Bindings)Merged into the same Bindings
WCMUsePojo subclassactivate() plus helpers such as getProperties()get(name, type)

Sling Model: parameters as request attributes

Sling passes parameters to Java objects as request attributes, so the model adapts from the request and injects them with @RequestAttribute:

package com.example.core.models;

import javax.annotation.PostConstruct;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.RequestAttribute;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

@Model(adaptables = SlingHttpServletRequest.class,
       defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ShortTitle {

    @ValueMapValue(name = "jcr:title")
    private String title;

    @RequestAttribute
    private Number maxLength;

    private String shortTitle = "";

    @PostConstruct
    protected void init() {
        int max = maxLength != null ? maxLength.intValue() : 60;
        String t = title != null ? title : "";
        shortTitle = t.length() > max ? t.substring(0, max) + "..." : t;
    }

    public String getShortTitle() {
        return shortTitle;
    }
}

The engine sets the attributes before creating the model and puts the old values back straight after. Read them through injection or in @PostConstruct; a getter that calls request.getAttribute() later finds the attribute gone. Request-attribute injection does no type conversion, so declare the field as Number and convert it yourself.

A model that adapts from Resource can't see the parameters at all. If it needs them, adapt from the request, as above, and read content with @ValueMapValue, which takes its values from the request's resource.

A POJO implementing Use

A Java Use object needs only a public no-argument constructor and an optional init(Bindings) method. Sling defines that method in org.apache.sling.scripting.sightly.pojo.Use, and passes the script's global bindings with the parameters merged in:

package com.example.core.use;

import javax.script.Bindings;

import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.scripting.sightly.pojo.Use;

public class ShortTitleUse implements Use {

    private String shortTitle = "";

    @Override
    public void init(Bindings bindings) {
        ValueMap properties = (ValueMap) bindings.get("properties");
        Object max = bindings.get("maxLength");
        int maxLength = max instanceof Number ? ((Number) max).intValue() : 60;
        String title = properties.get("jcr:title", "");
        shortTitle = title.length() > maxLength ? title.substring(0, maxLength) + "..." : title;
    }

    public String getShortTitle() {
        return shortTitle;
    }
}

A parameter with the same name as a global, such as resource, replaces it inside init(). Besides your parameters, the names you can read are the HTL global objects.

WCMUsePojo: AEM's ready-made Use class

com.adobe.cq.sightly.WCMUsePojo implements Use, stores the bindings, then calls your activate(). Its get(name, type) reads any binding, parameters included, and getters such as getCurrentPage() or getProperties() cover the common objects:

package com.example.core.use;

import com.adobe.cq.sightly.WCMUsePojo;

public class ShortTitlePojo extends WCMUsePojo {

    private String shortTitle = "";

    @Override
    public void activate() throws Exception {
        Number max = get("maxLength", Number.class);
        int maxLength = max != null ? max.intValue() : 60;
        String title = getProperties().get("jcr:title", "");
        shortTitle = title.length() > maxLength ? title.substring(0, maxLength) + "..." : title;
    }

    public String getShortTitle() {
        return shortTitle;
    }
}

The class isn't marked deprecated in the Cloud Service Javadoc, though Adobe's own Java Use-API example ends by recommending Sling Models in production.

Which one should a new component use?

A Sling Model, on both AEM as a Cloud Service and 6.5. HTL can load it by an interface, as the Core Components Text component does, and Sling Models then picks the implementation whose resourceType is closest to the rendered resource, so a project replaces the logic without editing the script. Keep WCMUsePojo classes that already work and rewrite them as Sling Models when the component changes for another reason. Use a bare Use POJO only for a small helper that needs nothing beyond the bindings.