HTL Java Use-API Code Example

The HTML Template Language (HTL) Java Use-API allows an HTL file to use data-sly-use to access helper functions in a custom Java class. This allows any sophisticated business logic to be wrapped in Java code, while the HTL code is just responsible for direct markup generation.

A simple POJO can be used as a Java Use-API object, which is instantiated by a specific implementation using the POJO’s default constructor.

In this article, you will find an example of how the Java Use-API can be bind with a component in AEM. In detail, we will be associating the Java Use-API with the header component.


Java Use-API Example Code

Step 1

We will place a small little snippet from lines:2-4 which will bind into the simple POJO; inside of the given component,
/apps/sourcedcode/components/structure/header/header.html

1
2
3
4
5
6
<div class="cmp-header">
  <div data-sly-use.header="HeaderJavaUseExample">
    <h1>${header.path}</h1>
  </div>
  <!--/* header Sightly HTL code implementation below */-->
</div>

Step 2

We will create a simple Java POJO under,
/apps/sourcedcode/components/structure/header/HeaderJavaUseExample.java

Taking a look closer, line:1 has the package that is the same path as the targeted component.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package apps.sourcedcode.components.structure.header;

import com.adobe.cq.sightly.WCMUsePojo;

public class HeaderJavaUseExample extends WCMUsePojo {


    @Override
    public void activate() throws Exception {

    }

    public String getPath() {
        return "This is an example path";
    }


}

Step 3

The Java Pojo extends the WCMUsePojo which makes available all the Global Objects in this naming convention:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
getComponent()
getComponentContext()
getCurrentDesign()
getCurrentNode()
getCurrentPage()
getCurrentSession()
getCurrentStyle()
getDesigner()
getEditContext()
getLog()
getOut()
getPageManager()
getReader()
getRequest()
getResolver()
getResource()
getResourceDesign()
getResourcePage()
getResponse()
getSling()
getSlyWcmHelper()
getWcmmode()
getXssAPI()

Hello, I am an enthusiastic Adobe Community Advisor and a seasoned Lead AEM Developer. I am currently serving as an AEM Technical Lead at MNPDigital.ca, bringing over a decade of extensive web engineering experience and more than eight years of practical AEM experience to the table. My goal is to give back to the AEM Full Stack Development community by sharing my wealth of knowledge with others. You can connect with me on LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back To Top
bacan4d
bacansport
bacan4d
bacan4d
bacansport
bacansport
xx1toto
bacansport
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
bacansport
xx1toto
bacan4d
xx1toto
xx1toto
bacan4d
linkbacan4d
bacan4d
bacan4d
bacansports
bacan4d
xx1toto
xx1toto
bacan4d
bacan4d
xx1toto
xx1toto
xx1toto