本文介绍了AEM6直观:如何将HTML中的参数传递给Java模型类中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将参数从html传递给WCMUse类。

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>

很遗憾,我似乎无法将任何参数传递给该方法。有什么方法可以将参数从html传递给WCMUse类?

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?

推荐答案

Java Use-API不支持将参数传递给吸气方法。您可以在Use类初始化期间传递一次参数。看一下受:

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代码:

// 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;
    }
}

这类参数仅在Use类时才有意义是从 data-sly-template 元素调用的(否则参数也可以在Use类中进行硬编码)。可以在。

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.

这篇关于AEM6直观:如何将HTML中的参数传递给Java模型类中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 05:52