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

Avatar

Avatar
Give Back 10
Level 3
karanmahi
Level 3

Likes

15 likes

Total Posts

73 posts

Correct reply

0 solutions
Top badges earned
Give Back 10
Give Back 5
Give Back 3
Give Back
Ignite 5
View profile

Avatar
Give Back 10
Level 3
karanmahi
Level 3

Likes

15 likes

Total Posts

73 posts

Correct reply

0 solutions
Top badges earned
Give Back 10
Give Back 5
Give Back 3
Give Back
Ignite 5
View profile
karanmahi
Level 3

02-05-2021

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)

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

275 likes

Total Posts

320 posts

Correct reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

275 likes

Total Posts

320 posts

Correct reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
Anudeep_Garnepudi
MVP

02-05-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#....

 

Answers (3)

Answers (3)

Avatar

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

645 likes

Total Posts

607 posts

Correct reply

239 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

645 likes

Total Posts

607 posts

Correct reply

239 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
BrianKasingli
MVP

03-05-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);

Avatar

Avatar
Springboard
Level 7
KiranVedantam1992
Level 7

Likes

195 likes

Total Posts

213 posts

Correct reply

60 solutions
Top badges earned
Springboard
Give Back 5
Ignite 1
Affirm 50
Validate 1
View profile

Avatar
Springboard
Level 7
KiranVedantam1992
Level 7

Likes

195 likes

Total Posts

213 posts

Correct reply

60 solutions
Top badges earned
Springboard
Give Back 5
Ignite 1
Affirm 50
Validate 1
View profile
KiranVedantam1992
Level 7

03-05-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

Avatar

Avatar
Applaud 25
Level 10
asutosh_jena
Level 10

Likes

593 likes

Total Posts

705 posts

Correct reply

207 solutions
Top badges earned
Applaud 25
Give Back 100
Boost 500
Affirm 100
Ignite 1
View profile

Avatar
Applaud 25
Level 10
asutosh_jena
Level 10

Likes

593 likes

Total Posts

705 posts

Correct reply

207 solutions
Top badges earned
Applaud 25
Give Back 100
Boost 500
Affirm 100
Ignite 1
View profile
asutosh_jena
Level 10

02-05-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!