April 7, 2020
Estimated Post Reading Time ~

Implement Use-Interface using Sightly in AEM 6.x

Implementing sightly can be basically done with two important APIs (although few others are also available). In my other article I have cover up how you can Access OSGI ser­vice from the WCMUse-class in Sightly, here we are going to cover next important one which shows how you can implement the User-interface in Java and combine your business logic with component file.

Let’s see Use interface first, later we will jump to code.

Looking to Java Documentation of Use,

All you need to make sure is to implement public void init(javax.script.Bindings bindings). The init method will be called on initialization of the class with a Bindings object that holds all the context objects and any parameters passed into the use-class. Business logic will be present in this method, although you are free to create custom method which will be called from init().

Binding object is an mapping of key/value pairs, all of whose keys are Strings.

Now its time to jump into code. we have created a simple component which will accept developer’s name, its favorite tool and a small description of himself.

1. In sightly component file we have following code
<div data-sly-use.data="${ 'com.adobeaemclub.adobeaemclub.core.services.DeveloperProfile'
@ devName=properties.devName, tool=properties.tool, about=properties.about}">
${data.profile @ context='html'}
</div>


Line 2: You can see we are passing 3 parameter’s, these parameters can be read via binding objects.

2. Implement Use interface and override init() method
import javax.script.Bindings;
import org.apache.sling.api.resource.Resource;
import io.sightly.java.api.Use;

public class DeveloperProfile implements Use {

 private String developer;

 @Override
 public void init(Bindings bindings) {
   // all standard objects/binding are available 

Line 5: Implementing Use Interface
Line 10: Overriding init() method provided by use interface

3. Read parameters passed from sightly component files
// parameters are passed as bindings
String developerName = (String) bindings.get("devName");
String tool = (String) bindings.get("tool");
String aboutMe = (String) bindings.get("about");

4. Here is component and the output can be seen


Output:


5. Here is the component Java file

package com.adobeaemclub.adobeaemclub.core.services;

import javax.script.Bindings;
import org.apache.sling.api.resource.Resource;
import io.sightly.java.api.Use;

public class DeveloperProfile implements Use {

 private String developer;

 @Override
 public void init(Bindings bindings) {
  // all standard objects/binding are available
  Resource resource = (Resource) bindings.get("resource");

  // parameters are passed as bindings
  String developerName = (String) bindings.get("devName");
  String tool = (String) bindings.get("tool");
  String aboutMe = (String) bindings.get("about");

  developer = "Our developer " + developerName.toUpperCase() +
   " works on " + tool.toUpperCase() +
   ", Here's whats he says about " + "himself\n\"" + aboutMe +
   "\"";

 }

 public String getProfile() {
  return developer;
 }
}

That’s all, all you need to implement an interface and using binding objects you can read values. The above code was tested in AEM 6.1. Let me know if you need any help with implementation.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.