beckchr / juel

Java Unified Expression Language
http://juel.sf.net
Apache License 2.0
133 stars 43 forks source link

Could not resolve function 'format' #98

Closed jhonvedo closed 4 years ago

jhonvedo commented 6 years ago

hi!!!!

I try to execute these sentences but it always returns this error "Could not resolve function 'format'"

ExpressionFactory factory = new de.odysseus.el.ExpressionFactoryImpl(); de.odysseus.el.util.SimpleContext context = new de.odysseus.el.util.SimpleContext();
ValueExpression e = factory.createValueExpression(context, "${format('Hey %s','Joe')}", String.class); System.out.println(e.getValue(context));

I have JUEL 2.2.7

help me please!!

juanmanuelz commented 6 years ago

Hello @jhonvedo It's great that you're learning juel :) Currently you need some more declarations to get your code working. Maybe you can look at http://juel.sourceforge.net/guide/advanced/index.html to haver a better understanding of this library. Also and with all my respect, this kind of questions are better for sites like StackOverflow. Now and willing to help, you can register the format function with a preffix. String format it's a nice utility because it can take a variable number of arguments. So you need to register the method using Object[].class (I'm not sure if it's the only way to do that).

This is a code sample for the stuff you're tryng:

public void juelTest() {
    try {
        ExpressionFactory factory = new de.odysseus.el.ExpressionFactoryImpl();
        de.odysseus.el.util.SimpleContext context = new de.odysseus.el.util.SimpleContext();
        //Register the format function with preffix "my"
        context.setFunction("my", "format", String.class.getMethod("format", String.class, Object[].class));
        ValueExpression e = factory.createValueExpression(context, "${my:format('Hey %s','Joe')}", String.class);
        System.out.println(e.getValue(context));
    } catch (NoSuchMethodException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (SecurityException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

Have an awesome coding day!