Search code examples
jsffaceletstaglibbcel

taglib call to managedbean call


i have an managed bean(session scope) like this:

class Home {// as homeBean
  public void doSomething(ActionEvent ae, int a, int b){
    System.out.println("result="+(a+b));
  }
}

i like to call this

<a4j:commandLink actionListener="#{homeBean:doSomething(1,2)}"/>

what i know is: it isnt possible to use a and b parameter.

ok: this should be in example a "static" possibility to invoke this using an taglib:

public class CoolTaglib implements TagLibrary{
   ...
  public static void doSomething(int a, int b) {
    getHomeBeanFromSession().doSomething(a,b);
  }
}

what about to invoke it dynamicly? using bcel or URLClassLoader?


Solution

  • This EL expression syntax is for static methods only and must be defined in a tag library and have the namespace defined in the view:

     #{namespacePrefix:fn(arg)}
    

    This EL expression with invokes a parameterized method on an object instance:

    #{someInstance.method(arg)}
    

    The second form is available in Expression Language 2.2 or above (Java EE 6.) Similar expressions are supported in some 3rd party JSF libraries prior to this.

    It is possible to look up a managed bean from a static method so long as it is executed within a JSF context:

    FacesContext context = FacesContext.getCurrentInstance();
    SomeBean someBean = context.getApplication()
                               .evaluateExpressionGet(context, 
                                                      "#{someBean}", SomeBean.class);
    

    This is not the ideal approach however. This code was written against JSF 2; prior versions use different dynamic lookup calls.

    If you need a bean in a static method, use an expression of the form:

    #{namespacePrefix:fn(someBean, 1, 2)}