AEM6 Sightly: How to pass a parameter from HTML to a method from Java-model class?

喜欢而已 提交于 2019-12-07 12:10:40

问题


I wanted to pass a parameter from html to WCMUse class.

Java:

public class ComponentHelper extends WCMUse {

    public void activate() throws Exception {}

    ...

    public String methodA(String parameter1) {
        ...
    }

    public String getParam() {
        String param = "";
        ...
        return param;
    }
}

HTML:

<componentHelper data-sly-use.componentHelper="ComponentHelper" data-sly-unwrap />
...
<div>
    ${componentHelper.methodA @ parameter1=componentHelper.param}
    <!--/* Also tried: ${componentHelper.methodA @ componentHelper.param} */-->
</div>

Unfortunately, it looks like I can't pass any parameter into the method. Is there any way to pass a parameter to WCMUse class from html?


回答1:


Java Use-API doesn't support passing parameters to the getter method. You may pass parameters once, during the Use class initialization. Take a look on this example inspired by the Sightly documentation:

<!-- info.html -->
<div data-sly-use.info="${'Info' @ text='Some text'}">
    <p>${info.reversed}</p>
</div>

Java code:

// Info.java
public class Info extends WCMUse {

    private String reversed;
     
    @Override
    public void activate() throws Exception {
        String text = get("text", String.class);
        reversed = new StringBuilder(text).reverse().toString();
    }
  
    public String getReversed() {
        return reversed;
    }
}

Such kind of parameters makes sense only when the Use class is invoked from data-sly-template elements (otherwise parameters could be as well hardcoded in Use class). More info can be found in the following chapter of aferomentioned documentation.



来源:https://stackoverflow.com/questions/28065552/aem6-sightly-how-to-pass-a-parameter-from-html-to-a-method-from-java-model-clas

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