Vineet Manohar is CTO / Co-Founder of online Car Search Engine, iSeeCars.com. Vineet is a DZone MVB and is not an employee of DZone and has posted 20 posts at DZone. You can read more from them at their website. View Full User Profile

Calling a Static Method From EL

08.18.2010
| 14261 views |
  • submit to reddit

In my previous post I showed how to pass parameters in EL methods. In this post, I will describe how to call static methods from EL.

Step 1: Download Call.java and ELMethod.java

Download the classes Call.java and ELMethod.java, and copy them to your project.

Step 2: Add an instance of Call.java as an application scoped bean

There are many ways to do this, depending on this technology you are using. Here are some examples:

How to set request scope in JSP

request.setAttribute("call", new Call());  

How to set session scope in JSP

session.setAttribute("call", new Call()); 
How to set application scope from Servlet
this.getServletContext().setAttribute("call", new Call())  

How to set application scope from JSP

 <jsp:useBean id="call" class="Call" scope="application" />  

How to set application scope in Seam

Add these annotations to your class

@Name("call")
@Scope(ScopeType.APPLICATION)

Step 3: Call any static method from EL as follows

Let us say you have a static method which formats date.

   package com.mycompany.util;  

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {
public static String formatDate(String format, Date date) {
return new SimpleDateFormat(format).format(date);
}
}

You can call the above static method from EL as follows. Here “account” is a request scoped bean with a Date property call “creationDate”.

 ${call["com.mycompany.util.DateUtils.formatDate"]["MMM, dd"][account.creationDate]}  

In general the format is:

  ${call["full.package.name.MethodName"][arg1][arg2][...]}

Note:
  • Use map notation with [square brackets] when passing arguments to ${call}
  • First argument is the full package name + “.” + method name
  • If the static method you are calling takes arguments, simply pass those arguments using more [square brackets]
  • Overloaded methods are not supported, i.e. you cannot call methods where two methods with the same name exist in the class

References

 

From http://www.vineetmanohar.com/2010/08/calling-static-methods-from-el/

Published at DZone with permission of Vineet Manohar, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Fab Mars replied on Wed, 2010/08/18 - 1:49pm

That was a tough exercise of style you did. Great to know it's possible.

But that's quite a lot of code, and people not used to JSF shouldn't believe everything is like that.

They should know there's always a simple solution in JSF: from EL, call another non-static method within the backing bean that contains only one line: the call to the static method. Nobody cares because your backing bean is there to support the page and it can have (almost) garbage methods like that.

And to that regard, the point of having static methods in a JSF backing at all becomes questionable.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.