While Calling Service Inside HTL I am getting error | Community
Skip to main content
Level 3
August 27, 2024
Solved

While Calling Service Inside HTL I am getting error

  • August 27, 2024
  • 2 replies
  • 1049 views

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 {
 
/**
* @2007960 OneTrust Id
*/
public String getOneTrustId();
 
/**
*
* @2007960 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;
 
@8220494(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
*
* @90521 OneTrustConfig serverConfiguration The parameters activated for the component
*/
@580286
protected void activate(final OneTrustConfig serverConfiguration) {
this.init(serverConfiguration);
}
 
/**
* Method used to init the service when updated
*
* @90521 OneTrustConfig serverConfiguration The parameters activated for the component
*/
@9182423
protected void update(final OneTrustConfig serverConfiguration) {
this.init(serverConfiguration);
}
 
/**
* This method assigns the configuration when the component is activated and updated.
*
* @90521 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);
}
 
@9944223
public String getOneTrustId() {
LOGGER.info("oneTrustId value INSIDE getOneTrustId ==== "+ oneTrustId);
return oneTrustId;
}
 
@9944223
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 {
 
/**
* @2007960 The OneTrust Id
*/
@AttributeDefinition(name = "OneTrust ID", description = "OneTrust ID for Cookie Banner")
String getOneTrustId();
 
/**
*
* @2007960 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
 

 

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

 

Thanks

Lakhman

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

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.

2 replies

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
August 27, 2024

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
subnaikAuthor
Level 3
August 27, 2024

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

 

 

narendiran_ravi
Level 6
August 27, 2024

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-configuration.html

kautuk_sahni
Community Manager
Community Manager
August 30, 2024

@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