How to call Sling Model Method with input parameter AEM

前端 未结 2 765
北荒
北荒 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 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.

    
    

    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 settings = new HashMap();
    
      @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;
    }
    

提交回复
热议问题