Expand my Community achievements bar.

SOLVED

While Calling Service Inside HTL I am getting error

Avatar

Level 4

Dear All,

 

When I am trying to call my service in my HTL like below then I am getting below error

 

org/apache/sling/scripting/sightly/apps/page/body_html.java:
Line 79, column 4095 : ai.core.service.OneTrustService cannot be resolved to a type

 

My Code is Below

 

************** SERVICE CLASS  ***************
 
package ai.core.service;
 
public interface OneTrustService {
 
/**
* @Return OneTrust Id
*/
public String getOneTrustId();
 
/**
*
* @Return Google Tag Manager (gtm) Id
*/
String getGtmId();
 
}
 
************** SERVICE IMPL CLASS  ***************
 
package ai.core.service;
 
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.metatype.annotations.Designate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import ai.core.config.OneTrustConfig;
 
@component(service = OneTrustService.class, immediate = true)
@Designate(ocd = OneTrustConfig.class)
public class OneTrustServiceImpl implements OneTrustService {
 
private String oneTrustId;
private String gtmId;
 
private static final Logger LOGGER = LoggerFactory.getLogger(OneTrustServiceImpl.class);
 
/**
* Method used to init the service when activated
*
* @Param OneTrustConfig serverConfiguration The parameters activated for the component
*/
protected void activate(final OneTrustConfig serverConfiguration) {
this.init(serverConfiguration);
}
 
/**
* Method used to init the service when updated
*
* @Param OneTrustConfig serverConfiguration The parameters activated for the component
*/
protected void update(final OneTrustConfig serverConfiguration) {
this.init(serverConfiguration);
}
 
/**
* This method assigns the configuration when the component is activated and updated.
*
* @Param OneTrustConfig serverConfiguration The configuration set for the component
*/
private void init(final OneTrustConfig serverConfiguration) {
this.oneTrustId = serverConfiguration.getOneTrustId();
LOGGER.info("oneTrustId value ==== "+ oneTrustId);
this.gtmId = serverConfiguration.getGtmId();
LOGGER.info("gtmId value ==== "+ gtmId);
}
 
public String getOneTrustId() {
LOGGER.info("oneTrustId value INSIDE getOneTrustId ==== "+ oneTrustId);
return oneTrustId;
}
 
public String getGtmId() {
LOGGER.info("oneTrustId value INSIDE getGtmId ==== "+ gtmId);
return gtmId;
}
 
}
 
************** CONFIG CLASS  ***************
 
package ai.core.models;
 
import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;
 
/**
 *
 * This interface represents the configuration for OneTrust
 *
 */
@ObjectClassDefinition(name = "Talent Marketing OneTrust Config", description = "Talent Marketing OneTrust Config")
public @interface OneTrustConfig {
 
/**
* @Return The OneTrust Id
*/
@AttributeDefinition(name = "OneTrust ID", description = "OneTrust ID for Cookie Banner")
String getOneTrustId();
 
/**
*
* @Return The Google Tag Manager (GTM) Id
*/
@AttributeDefinition(name = "GTM ID", description = "GTM ID For Cookie Banner")
String getGtmId();
 
}
 
***************** HTL CODE *******************
 
<sly data-sly-use.templatedContainer="com.day.cq.wcm.foundation.TemplatedContainer"
     data-sly-repeat.child="${templatedContainer.structureResources}"
     data-sly-resource="${child.path @ resourceType=child.resourceType, decorationTagName='div'}"></sly>
 
 
<!--<p data-sly-use.gtmonetrustid="ai.marcel.contentadmin.core.service.OneTrustService">
 
   ${gtmonetrustid.gtmId}
 
</p> -->
 
<!-- Google Tag Manager (noscript) -->
<noscript>
    <iframe src="https://www.googletagmanager.com/ns.html?id=GTM-TF5QLQDS" height="0" width="0"
            style="display:none;visibility:hidden" title="Google tag manager">
    </iframe>
</noscript>
<!-- End Google Tag Manager (noscript) -->
 
*******************************
My server config file value is below
 
subnaik_0-1724764882979.png

 

Can anyone please help me here that why I am not getting the osgi config value in my HTL ?

 

Thanks

Lakhman

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @subnaik 

To call an OSGi service from a Sling Model and use that Sling Model in HTL in AEM, follow these steps:

  1. Create an OSGi Service:

    • Define an interface and implement the service.
    • Register the service with the OSGi framework.
  2. Create a Sling Model:

    • Inject the OSGi service into the Sling Model using the @OSGiService annotation.
    • Expose the model’s data through methods that use the service.
  3. Use the Sling Model in HTL:

    • Adapt the resource to the Sling Model using the data-sly-use directive.
    • Render the model’s data in HTL using expressions.


Arun Patidar

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

Hi @subnaik 

To call an OSGi service from a Sling Model and use that Sling Model in HTL in AEM, follow these steps:

  1. Create an OSGi Service:

    • Define an interface and implement the service.
    • Register the service with the OSGi framework.
  2. Create a Sling Model:

    • Inject the OSGi service into the Sling Model using the @OSGiService annotation.
    • Expose the model’s data through methods that use the service.
  3. Use the Sling Model in HTL:

    • Adapt the resource to the Sling Model using the data-sly-use directive.
    • Render the model’s data in HTL using expressions.


Arun Patidar

Avatar

Level 4

Is there a way that we can directly call the below value Onetrust Id and GTM Id directly in HTL ?

 

subnaik_0-1724769505340.png

 

Avatar

Level 6

As Arun suggested, please inject your service in the sling model and get the values in HTL. If these values change based on the site then you can use context-aware configuration. Using ${caconfig['x.y.z.ConfigSample'].stringParam}, you can easily get the values in sightly without creating any helperModel. Refer https://sling.apache.org/documentation/bundles/context-aware-configuration/context-aware-configurati...

Avatar

Administrator

@subnaik Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!



Kautuk Sahni