how to call sling model only once

佐手、 提交于 2019-12-13 03:53:08

问题


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

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

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"

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?


回答1:


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

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

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


来源:https://stackoverflow.com/questions/53204646/how-to-call-sling-model-only-once

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!