Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I call a method named get() from EL?

 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Google Guava library has a nice addition, a wrapper class named Optional. You can use it as a return argument type when you might be returning a null value. Something llke:




The problem is that in the world of EL, a method that returns a value, has an implicit "get" prefix to the name, so you would take Java such as:

int tmp = foo.getValue();

and use

${foo.value}

What do I use when there is only the word "get", so there is no word "value" for EL to imply?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't follow the JavaBeans conventions (silly google), so it's not accessible to the EL.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. The most modern version of the EL (Tomcat 7) allows general method calls I believe, but I haven't tested that myself.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18212
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:P.S. The most modern version of the EL (Tomcat 7) allows general method calls I believe, but I haven't tested that myself.


Yep. It may even be in JSF2 on Tomcat6. While I frequently and vocally discourage putting logic on View definitions, rules are made to be broken - providing you know what you're doing and the rules won't let you do it within the rules.

The normal EL POJO access is "#{bean.propname}" or "${bean.propname}" which will delegate to the BeanUtils getPropname(). But if you code "#{bean.get()}" it should invoke the bean's method that has the signature "public typename get()" or "public void get()" when rendering the View. You will have to have a way for "typename" to be automatically converted to a String via BeanUtils, though.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic