如何实例化具有多个自适应的Sling模型

时间:2015-06-14 22:24:56

标签: cq5 aem sling sling-models

例如,Sling Models中的@Model注释允许多种适应性      @Model(adaptables = { SlingHttpServletRequest.class, Resource.class }) 但是,我不确定如何使用JSP中的多个自适应实例化模型。 Sling文档中显示的选项始终仅指定一个自适应:https://sling.apache.org/documentation/bundles/models.html#adaptto

1 个答案:

答案 0 :(得分:2)

When your model is adaptable from both classes it means you can use any of them, not that you have to adapt both.

So, you adapt it as any other Sling Model. Just it should work with both. In you case you could do

<sling:adaptTo adaptable="${resource}" adaptTo="org.apache.sling.models.it.models.MyModel" var="model"/>

or

<sling:adaptTo adaptable="${slingRequest}" adaptTo="org.apache.sling.models.it.models.MyModel" var="model"/>

Still, remember that if you are using injection, not all injectors are available from both adaptables. The request supports more than the resource (anything that comes from the script bindings, currentPage,etc)