This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL Functions

 
Nikhil Jain
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does a EL Function can call only a static method in a plain old java class. Can it not call other instance methods...?

Shashank
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does a EL Function can call only a static method in a plain old java class. Can it not call other instance methods...?

This is due to the way in which the container locates the methods through reflection... if it weren't to locate a static method, it would have to create a new instance of a class (through a default no-arg constructor since there's no other data allowing it to use a constructor with arguments), and then invoke method(s) on that. This introduces a performance hit because each time a new EL function was invoked, a new instance would need to be created in case a change in state of that instance had occured between invocations.

The purpose of an EL function is to perform some atomic operation which can manipulate its argument(s). EL functions aren't supposed to have maintain state or rely on other resources - so instance methods would be inappropriate. You shouldn't really use EL functions for anything other than the case where all the implementation can be done inside the single static method (perhaps delegating to other static methods elsewhere). Although your implementation method can of course use other object instances, it shouldn't keep them for any time longer than the duration of the method.

If you want to use instance methods, or indeed perform high-level things like database connections, you are much better with a JavaBean model or a set of custom tags. Try to keep EL functions aside for only really basic functionality (such as those in the JSTL Functions library).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic