Overloading the EL function defined in TLD

Is it possible to overload (function name) EL function? See the following TLD snippet:

Same function name rollDice

 <function> <name>rollIt</name> <function-class>com.Person</function-class> <function-signature>int rollDice()</function-signature> </function> <function> <name>rollIt</name> <function-class>com.Person</function-class> <function-signature>int rollDice(int)</function-signature> </function> 
+4
source share
2 answers

No, EL functions, unfortunately, do not support method overloading (or varargs). Give each function a different name.

+2
source

You cannot do this in a given TLD function, but you can put an object in the application area (once, at startup) using the varags method and / or overloaded methods and work fine.

Instead of defining it in a TLD, you can create an instance of your class in your init() method and place it in the application (servlet) area, for example:

 MyDiceRoller roller = new MyDiceRoller(); ServletContext context = getServletContext(); context.setAttribute("roller", roller); 

Then, instead of ${my:rollIt(6)} you do ${roller.rollIt(6)} . And instead of ${my:rollIt()} you would do ${roller.rollIt()} .

0
source

Source: https://habr.com/ru/post/1499472/


All Articles