问题描述
我想知道是否有一种简单的方法可以在 AEM 中获取带有吊索内容导出器结果的字符串.在我当前的用例中,我需要组件的 htl 文件中组件的 .model.json 输出的内容,并且发送额外的请求显然不是一个好主意.关于如何获取数据的任何提示?
I wonder if there is an easy way to get a String with the result of a sling content exporter in AEM.In my current usecase I need the content of a component's .model.json output in the component's htl file and sending an additional request is obviously not a good idea.Any hints on how I can get the data?
推荐答案
经过一些阅读和实验,我找到了一种方法:
After some reading and experimenting, I found a way to do it:
在你的 pom 中添加对以下包的依赖:
Add a dependency to the following package in your pom:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
然后在您的模型中创建一个执行序列化的方法:
Then create a method in your model that does the serialization:
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public String getJson() {
ObjectMapper objectMapper = new ObjectMapper();
String tStr = "";
try {
tStr = objectMapper.writeValueAsString(this);
logger.error(tStr);
}
catch (JsonProcessingException ex) {
logger.error("Cannot do it: {}", ex.getMessage());
}
return tStr;
}
现在您可以从 HTL 脚本或任何其他可以访问模型的代码片段中调用此方法.
Now you can call this method from inside a HTL script or any other code fragment that has access to the model.
这篇关于获取 .model.json 作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!