How to call Sling Model Method with input parameter AEM

六眼飞鱼酱① 提交于 2020-04-07 09:16:12

问题


I am having a scenario in which i want to call a sling model with input parameter. For this i have a code like this

<div data-sly-use.model3="${'com.bhf.aem.sling.models.Test' @ colour='red'}">
</div>

But I want to call a method in sling model twice with two different parameters .Is it possible with sling models?

Any Help!!!


回答1:


From AEM 6.3 there is a new HTL feature that allows to do this.

In the data-sly-include and data-sly-resource you can now pass requestAttributes in order to use them in the receiving HTL-script. This allows you to properly pass-in parameters into scripts or components.

<sly data-sly-use.settings="com.adobe.examples.htl.core.hashmap.Settings" 
        data-sly-include="${ 'productdetails.html' @ requestAttributes=settings.settings}"/>

Java-code of the Settings class, the Map is used to pass in the requestAttributes:

public class Settings extends WCMUsePojo {

  // used to pass is requestAttributes to data-sly-resource
  public Map<String, Object> settings = new HashMap<String, Object>();

  @Override
  public void activate() throws Exception {
    settings.put("layout", "flex");
  }
}

For example, via a Sling-Model, you can consume the value of the specified requestAttributes. In this example, layout is injected via the Map from the Use-class:

@Model(adaptables=SlingHttpServletRequest.class)
public class ProductSettings {

  @Inject @Optional @Default(values="empty")
  public String layout;
}



回答2:


By design of the HTL/Sightly language, sending parameters is only possible for data-sly-use (use objects initialization) and data-sly-call (template calls). The reason for this is to separate business logic from the view.

As mentioned by @tomasz-szymulewski, since https://issues.apache.org/jira/browse/SLING-5812, there is support for passing request attributes on resource/script inclusion in the Sling/AEM implementation.



来源:https://stackoverflow.com/questions/42324000/how-to-call-sling-model-method-with-input-parameter-aem

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