jsfstatic-methodsel

I can invoke static methods in EL, is this as per the spec?


I am confused about the answer of this question regarding static method calls in JSF EL.

I have this class with a public static method in a @Named bean:

@Named
public class StaticMethods {
    private static final ILogger logger =  LogFactory.getLogger(StaticMethods.class);
    
    public static void doSomething() {
        logger.info("called static void doSomething()");   
    }
}

And in my JSF page I use it like this:

#{staticMethods.doSomething()}

And the method gets called as shown in WildFly server.log:

09:47:22,977 INFO [Lumin-WEB#default task-32] Chrome  2bfb483b-c5c1-4079-a3bf-1dd13ec4425b a.l.m.w.u.StaticMethods: called static void doSomething()

How does this behavior correspond to the accepted answer and also to EL specification, where it mentions that a class has to be explicitly imported before its static methods can be referenced?

Is the reason that this works in my case, because I am using bean-discovery-mode="all" in my beans.xml ?

My WildFly-23.0.2.Final is using Mojarra 2.3.14.SP04 which implements jboss-el-api_3.0_spec


Solution

  • It's indeed supported since EL version 3.0, but only when you use the method expression syntax with the parenthesis () as in #{bean.method()}. In older EL versions you had to create a custom EL function.

    where it mentions that a class has to be explicitly imported before its static methods can be referenced

    That only applies to static fields, not to static methods.

    Is the reason that this works in my case, because I am using bean-discovery-mode="all" in my beans.xml ?

    No, the bean discovery mode has no influence whatsoever on EL property resolving. It has only influcence in whether the base (the #{bean} part of #{bean.method()}) can be resolved at all.