tags:

views:

163

answers:

2

Is it possible to overload an EL method in JSF 1.1 using Facelets as your view handler? If so, how?

For example, I have this code defining my EL methods (which are defined in namespace k):

public static String doStuff( String s ) {
    return doStuff( null, s );
}

public statis String doStuff( Map<String,String> m, String s ) {
    ...
    return something;
}

When I try to call #{k:doStuff("hey!")} from my Facelets page, I get this error:

Function 'k:doStuff' specifies 2 params, but 1 was declared
A: 

It looks like the problem was with how it was declared. For example, I was using this to declare my methods:

public class KTagLib extends AbstractTagLibrary {
    public static final String NAMESPACE = "http://mysite.blah/tags";
    public static final KTagLib INSTANCE = new KTagLib();

    public KTagLib() {
        super(NAMESPACE);
        try{
            try {
                Method[] methods = KTags.class.getMethods();

                for (int i = 0; i < methods.length; i++) {
                    if (Modifier.isStatic(methods[i].getModifiers())) {
                        this.addFunction(methods[i].getName(), methods[i]);
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }            
        }
    }

and using the following configuration:

<?xml version="1.0"?>
<!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "http://java.sun.com/dtd/facelet-taglib_1_0.dtd"&gt;
<facelet-taglib>
    <library-class>mypackage.KTagLib</library-class>
</facelet-taglib>

However, the this.addFunction() is essentially calling put() on a java.util.Map object so that duplicate methods can't be added since the keys are the same between doStuff.

To solve this problem, I'll have to explicitly declare the methods in the *.taglib.xml unless anyone knows of a way to dynamically solve the problems.

JavaNinja
+1  A: 

It turns out that no matter how you declare the function, it is being put in a Map with its name used as a key. So - no function overloading.

However, you can define the name of the function in the XML to be different, and then you can have overloaded methods. It's a bit counter-intuitive. But then your functions will be accessible via different names in the pages.

You can achieve the same thing dynamically, by supplying suffixes to overloaded method names (which you put in the map). Either way it's not such a good solution.

Bozho