Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Sightly - Issue while passing parameters to Java Use-Api

Avatar

Level 4

Hi,

 Issue while passing parameters to Java Use-Api

I tried with both the approach (Passing parameters to a use-class upon initialization and Passing parameters from data-sly-template) as mentioned on http://docs.adobe.com/docs/en/aem/6-0/develop/sightly/use-api-in-java.html

In both the cases i am not getting any result.

But If I access the java use api without passing parameters as like <div data-sly-use.info="Info"> its working fine.

Code for reference:

Info.java

package apps.testsightly.components.content.sightlyComponent;

import com.adobe.cq.sightly.WCMUse;

public class Info extends WCMUse {
    private String lowerCaseTitle;
    private String lowerCaseDescription;

    private String reverseText;
  
    @Override
    public void activate() throws Exception {
        lowerCaseTitle = "lowercaseTitle";
        lowerCaseDescription = "lowerCaseDescription";

        String text = get("text", String.class);
        reverseText = new StringBuffer(text).reverse().toString();

 

    }
  
    public String getLowerCaseTitle() {
        return lowerCaseTitle;
    }
  
    public String getLowerCaseDescription() {
        return lowerCaseDescription;
    }

    public String getReverseText()
    {

   return reverseText; 
    }
}

sightlyComponent.html

Test Sightly

 

Without Parameter

<div data-sly-use.info="Info">

  <h1>${info.lowerCaseTitle}</h1>
  <p>${info.lowerCaseDescription}</p>
     

</div>

With parameter    

    <div data-sly-use.info1="${Info @ text='Some text'}">

  <h1>${info1.lowerCaseTitle}</h1>
  <p>${info1.lowerCaseDescription}</p>
  <p>${info1.reverseText}</p>
     

</div>    

Withparameter Using Template

<div data-sly-use.info2="Info"
     data-sly-use.extra="extra.html">
     
  <h1>${info2.lowerCaseTitle}</h1>
  <p>${info2.lowerCaseDescription}</p>
     
  <div data-sly-call="${extra @ text=properties.testfield}"></div>
 
</div>

 

Extra.html

<template data-sly-template.extra="${@ text}"
          data-sly-use.extraHelper="${ExtraHelper @ text=text}">
  <p>${extraHelper.reversedText}</p>
</template>

Extra Helper.java

package apps.testsightly.components.content.sightlyComponent;

import org.apache.sling.api.resource.ModifiableValueMap;

import com.adobe.cq.sightly.WCMUse;

package apps.my_example.components.info;
  
import com.adobe.cq.sightly.WCMUse;
  
public class ExtraHelper extends WCMUse {
    private String reversedText;

     
    @Override
    public void activate() throws Exception {
        String text = get("text", String.class);      
        reversedText = new StringBuilder(text).reverse().toString();


    }
  
    public String getReversedText() {
        return reversedText;
    }
}

 

Output:

lowercaseTitle

lowerCaseDescription

With parameter

 

 

 

Withparameter Using Template

lowercaseTitle

lowerCaseDescription

Thanks,

Karthi

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Sure! When passing parameters from Sightly to a Java Use-API class, you need to create a Java class that implements the Use interface. The Use interface allows you to process data in your Java class and pass parameters from Sightly to the Java class.

Here's an example of the Java code:

Java Use-API class (SampleUseClass.java):

package com.example.core.models;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.scripting.SlingScriptHelper;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.apache.sling.models.annotations.Model;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.Map;

@Model(adaptables = Resource.class)
public class SampleUseClass implements Use {

    @inject
    private String param1;

    @inject
    private String param2;

    // Constructor to initialize the parameters
    public SampleUseClass(String param1, String param2) {
        this.param1 = param1;
        this.param2 = param2;
    }

    @Override
    public void init(Bindings bindings) {
        // You can process the parameters here or perform other tasks with Sightly data
    }
    
    // Method to get the result based on parameters
    public String getResult() {
        // Process the parameters and return a result
        return "Result based on param1: " + param1 + " and param2: " + param2;
    }
}

In the above Java class, we create a constructor to initialize the parameters (param1 and param2) that we want to receive from Sightly. The init method allows you to perform additional initialization and data processing if required.

Now, let's see how to pass parameters from Sightly to the Java Use-API class:

Sightly (HTML) code:

<sly data-sly-use.sampleUseClass="com.example.core.models.SampleUseClass"
     data-sly-unwrap>
    <!-- Pass the parameters to the Java class -->
    <h1>${sampleUseClass.getResult @ param1='Value1', param2='Value2'}</h1>
</sly>

In the Sightly code, we call the getResult method of the SampleUseClass and pass the parameters param1 and param2 with their respective values ('Value1' and 'Value2').

When you render the Sightly component, it will invoke the Java Use-API class with the provided parameters and display the result in the <h1> tag. In this example, the output will be "Result based on param1: Value1 and param2: Value2".

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Sure! When passing parameters from Sightly to a Java Use-API class, you need to create a Java class that implements the Use interface. The Use interface allows you to process data in your Java class and pass parameters from Sightly to the Java class.

Here's an example of the Java code:

Java Use-API class (SampleUseClass.java):

package com.example.core.models;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.scripting.SlingScriptHelper;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.apache.sling.models.annotations.Model;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.Map;

@Model(adaptables = Resource.class)
public class SampleUseClass implements Use {

    @inject
    private String param1;

    @inject
    private String param2;

    // Constructor to initialize the parameters
    public SampleUseClass(String param1, String param2) {
        this.param1 = param1;
        this.param2 = param2;
    }

    @Override
    public void init(Bindings bindings) {
        // You can process the parameters here or perform other tasks with Sightly data
    }
    
    // Method to get the result based on parameters
    public String getResult() {
        // Process the parameters and return a result
        return "Result based on param1: " + param1 + " and param2: " + param2;
    }
}

In the above Java class, we create a constructor to initialize the parameters (param1 and param2) that we want to receive from Sightly. The init method allows you to perform additional initialization and data processing if required.

Now, let's see how to pass parameters from Sightly to the Java Use-API class:

Sightly (HTML) code:

<sly data-sly-use.sampleUseClass="com.example.core.models.SampleUseClass"
     data-sly-unwrap>
    <!-- Pass the parameters to the Java class -->
    <h1>${sampleUseClass.getResult @ param1='Value1', param2='Value2'}</h1>
</sly>

In the Sightly code, we call the getResult method of the SampleUseClass and pass the parameters param1 and param2 with their respective values ('Value1' and 'Value2').

When you render the Sightly component, it will invoke the Java Use-API class with the provided parameters and display the result in the <h1> tag. In this example, the output will be "Result based on param1: Value1 and param2: Value2".