how to add inline config value to html file and java file from cfg.json file in AEM as a cloud service | Community
Skip to main content
May 5, 2022
Solved

how to add inline config value to html file and java file from cfg.json file in AEM as a cloud service

  • May 5, 2022
  • 2 replies
  • 1740 views

hi, I want my variable which I have declared in cfg.json file to be used instead of static value in HTML file .please help me how to do it.

my cfg.json file is : 

{
"cssCds": "https://www.example.css",
"jsCds":"https://www.example.js"
}

I am made one configuration java file which is :


@Component(service = ProductDetailsConfiguration.class)
@Designate(ocd = ProductDetailsConfiguration.Configuration.class)
public class ProductDetailsConfiguration {
private String cssCds;
private String jsCds;


@ObjectClassDefinition(name = "configuration Service")
@interface Configuration {
@AttributeDefinition(name = "Css Cds", type = AttributeType.STRING, description = "URL for css file of CDS")
String cssCds() default "https://www.example.css";
@AttributeDefinition(name = "Js Cds", type = AttributeType.STRING, description = "URL for js file of CDS")
String jsCds() default "https://www.example.js";

}

@Activate
@Modified
protected void activate(final ProductDetailsConfiguration.Configuration cfg) {
this.cssCds = cfg.cssCds();
this.jsCds = cfg.jsCds();

}

public String getCssCds() {
return this.cssCds;
}
public String getJsCds() {
return this.jsCds;
}

}


I have one HTML file where I want to use these variables instead of hardcoded URLs 

for e.g.: 

<link href="https://www.example.css" rel="stylesheet" type="text/css">
// I want to replace "https://www.example.css" with the variable defined in cfg.json file that is "cssCds"
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,

Create a Sling Model

Read that service configuration in the Sling Model

Use sling model in HTL file to read domains

2 replies

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
May 5, 2022

Hi,

Create a Sling Model

Read that service configuration in the Sling Model

Use sling model in HTL file to read domains

Arun Patidar
May 5, 2022

@arunpatidar  hi , I am getting stuck while getting service config data to model file

for eg : 

public ProductDetailsConfiguration productDetailsConfiguration = new ProductDetailsConfiguration();

 but I am getting null for :

log.info("ProductDetailsComponent test example: {}",productDetailsConfiguration.getCssCds());//it's giving null value

 is there any other configuration that needs to be done between cfg.json and configuration.java file ?

May 5, 2022

was able to resolve the issue thank you.

 

@Reference
public
ProductDetailsConfiguration productDetailsConfiguration;

 

 

Himanshu_Jain
Community Advisor
Community Advisor
May 5, 2022

Define env key/Value in the cfg.json and define the values in cloud manager config console for every environment.

Refer: https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/implementing/deploying/configuring-osgi.html?lang=en#inline-values

 

Himanshu Jain