如何仅调用吊索模型一次 [英] how to call sling model only once

查看:86
本文介绍了如何仅调用吊索模型一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个组件A和一个组件B。
在组件A中,我正在编写html文件

Lets say I have a component A and component B. In component A, html file I am writing

   data-sly-use.head="com.Mymodel"

现在在组件B中,我也需要使用相同的类,因此在html文件中,我编写了

now in Component B also I need to use the same class, so in html file I have written

   data-sly-use.head="com.Mymodel"

组件A和组件B可以在页面中可用,也可以一次不可用。我想要的是,如果页面中同时存在组件A和组件B,那么我就不想两次初始化Sling模型(com.Mymodel)。我只想初始化一次。怎么做?

Component A and Component B can be available in a page or may not be available at a time. What I want is that if both Component A and B present in a page then I do not want to initialise Sling model (com.Mymodel) twice. I would like to initialise only one time. How to do it?

推荐答案

看看Sling Model Caching。只要它可以从Resource适应,就可以满足您的要求。

Take a look at Sling Model Caching. This does exactly what you want, as long as it is adaptable from Resource.

https://sling.apache.org/documentation/bundles/models.html#caching

您只需在模型注释中指定 cache = true

You only have to specify cache=true in the Model-annotation.

@Model(adaptable = SlingHttpServletRequest.class, cache = true)
public class ModelClass {
   ...
}

这篇关于如何仅调用吊索模型一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆