AEM Slightly - How to call java method whose name is stored in variable - expression inside expression doesn’t work in sightly | Community
Skip to main content
May 3, 2021
Solved

AEM Slightly - How to call java method whose name is stored in variable - expression inside expression doesn’t work in sightly

  • May 3, 2021
  • 4 replies
  • 5369 views

In AEM 6.5 sightly I have initialized the sling model class. Now, I want to call the method from that class. But method name is read from some variable (basically I read method name from other location). When I use that variable it doesn’t work. I know sightly doesn’t allow expression inside an expression, so would like to know if there is an alternative to fit this need.

<sly data-sly-use.detailsModel="org.svc.core.model.DetailsModel"/>

${detailsModel.{methodNameVariable}} - doesn’t work (if the method name is read from some  variable) 

 

${detailsModel.methodName} - works (if put the method name directly there)

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Anudeep_Garnepudi

@karanmahi 

I think this is not directly possible. One way you can do is, in your Model class itself call the method inside a getter method and return the value. Call that Sling Model getter method in HTL.

To call method using method name(String/Variable) use reflection API.

Refer: https://www.tutorialspoint.com/How-do-I-invoke-a-Java-method-when-given-the-method-name-as-a-string#:~:text=You%20can%20invoke%20the%20method,the%20form%20of%20a%20string.

 

4 replies

Asutosh_Jena_
Community Advisor
Community Advisor
May 3, 2021

Hi @karanmahi 

 

What is the requirement where you are reading the method name from a variable?

You need to call the method directly by the sling model reference to make it work.

<sly data-sly-use.detailsModel="org.svc.core.model.DetailsModel"/>
${detailsModel.methodName}

 

Thanks!

karanmahiAuthor
May 3, 2021
I have a custom requirement to read method name dynamically from osgi.
Anudeep_Garnepudi
Community Advisor
Anudeep_GarnepudiCommunity AdvisorAccepted solution
Community Advisor
May 3, 2021

@karanmahi 

I think this is not directly possible. One way you can do is, in your Model class itself call the method inside a getter method and return the value. Call that Sling Model getter method in HTL.

To call method using method name(String/Variable) use reflection API.

Refer: https://www.tutorialspoint.com/How-do-I-invoke-a-Java-method-when-given-the-method-name-as-a-string#:~:text=You%20can%20invoke%20the%20method,the%20form%20of%20a%20string.

 

Kiran_Vedantam
Community Advisor
Community Advisor
May 3, 2021

Hi @karanmahi,

 

If you want to read the method name, create a variable, assign the method name to that variable and fetch it via getter and setter in HTL.

 

Hope this helps.

 

Thanks,

Kiran Vedantam

BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
May 3, 2021

@karanmahi you can have your @PostConstruct method invoke your method which will set the variable's value. OR you can ensure that your method is following this naming convention getMyMethod(); from sightly you can call ${detailsModel.myMethod}.

Anything getSomething(); sightly will allow you to call the method in camelCased (in this example ${detailsModel.something);