Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

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

Avatar

Level 5

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"
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

Create a Sling Model

Read that service configuration in the Sling Model

Use sling model in HTL file to read domains



Arun Patidar

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

Hi,

Create a Sling Model

Read that service configuration in the Sling Model

Use sling model in HTL file to read domains



Arun Patidar

Avatar

Level 5

@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 ?

Avatar

Level 5

was able to resolve the issue thank you.

 

@Reference
public
ProductDetailsConfiguration productDetailsConfiguration;

 

 

Avatar

Community Advisor

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/deploy...

 

Himanshu Jain