jsfeljsf-2.2methodexpression

How to bind a bean's method with package in actionListener?


I am pretty much newbie to Java/JSF technology and I've been trying to do simple UIs to understand how things are going on in Java/JSF world.

Now I want to bind a ManagedBean's method with its package name to a CommandButton's actionListener.

Example:

index.xhtml

...
<h:commandButton actionListener="#{com.acme.myclass.MyMethod}" ...
...

MyClass.java

package com.acme;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped

public class MyClass {

    public MyClass() {
        // Constructor
    }

    public void MyMethod() {
        // Do some job
    }

}

I tried to bind the method as the given example above but the method is not called. Besides I don't see my packages in Netbeans's little autocomplete window. If I bind the method in #{class.method} format (as all given examples on the internet) it works.

Is there a way to achieve this?

Why do I need to do this?

While projects are getting larger and larger naming classes getting harder and harder. So, I think that same class names in different packages make life easier.


Solution

  • JSF doesn't permit that kind of actionlistener binding, simple. The only semblance to what you have in the JSF world, is a standalone implementation of the ActionListener interface, with which you can then supply a FQN:

      <h:commandButton>
           <f:actionListener type="com.you.YourActionListenerImpl" />
      </h:commandButton>
    

    Where YourActionListenerImpl will be a class implementing ActionListener as mentioned previously

    As to why you want to do this, I think you're overstating the cost of naming - it's not that hard. Massive, sprawling projects are getting along just fine (look at the Spring project for example) with the naming of Java artifacts.