How to call Sling Model Method with input parameter AEM

前端 未结 2 757
北荒
北荒 2021-01-27 09:32

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

相关标签:
2条回答
  • 2021-01-27 09:52

    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.

    0 讨论(0)
  • 2021-01-27 10:08

    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;
    }
    
    0 讨论(0)
提交回复
热议问题