Use-interface

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 implements the Use-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

 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

Line 5: Implementing Use Interface

Line 10: Overriding init() method provided by use interface

3. Read parameters passed from sightly component files

4. Here is component and the output can be seen

Use-Interface componentOutput:

Use-Interface component output

5. Here is the component Java file

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

7 thoughts on “Implement Use-Interface using Sightly in AEM 6.x

  1. Why binding? We have getProperties() method to read your properties. It’s available OOTB (from Use). Just to read properties I don’t think we should go with the binding.

     
      1. Hi Abani,
        Article above only shows a simple implementation of use-interface. It’s main purpose is to help one learn its implementation.
        I do agree with you it does not show the full capability of bindings as it wasn’t the main motive…
        I hope this answers your question.

        Thanks

         
        1. Yes I understood that. We can have a proper usage example of bindings.. If you want I can write up something on this.. Anyways thanks for the reply and for the article

           
          1. Hi Abani,
            We would be really happy if you would like to contribute to this club with your learnings and findings !

            Thanks!

             
  2. Hi Abani,

    Thank you very much for this info. Could you please share the folder structure of this implementation.I am new to AEM. Till now I have worked on implementing Templates,Components. I am not sure where to include JAVA files. So pls share your work folder structure.

    Thanks again.

    Regards
    Krishh

     

Leave a Reply

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