How to write juel expression

Andreas Chandra Gaozu :

Recently I try out JUEL, and now I get confused how to write several methods which i can't find the example in the documentation.

It's only give me this http://juel.sourceforge.net/guide/start.html

and here what I wanna know how to write it out if I have context.setFunction("meh", "max", BigDecimal.class.getMethod("compareTo", BigDecimal.class));

since we know bigDecimal expression is write like foo.compareTo(bigDecimal); how to write this in the expression?

Andreas Chandra Gaozu :

Done easily with 2 possible answers

  1. Since SimpleContext let you do arithmetical calculations (in my case), i simply put the calculation here. I also use ValueExpression (not in the right place i guess) to provide me mapped value from SimpleContext. Thus, here what i have

    context.setVariable("fii", factory.createValueExpression(new BigDecimal(3), BigDecimal.class));
    context.setVariable("fee", factory.createValueExpression(new BigDecimal(5), BigDecimal.class));
    
    ValueExpression e1 = factory.createValueExpression(context, "${fee}", BigDecimal.class);
    ValueExpression e2 = factory.createValueExpression(context, "${fii}", BigDecimal.class);
    
    String temp1 = (String)e1.getValue(context).toString();
    String temp2 = (String)e2.getValue(context).toString();
    
    context.setVariable("foo", factory.createValueExpression(new BigDecimal(temp1).add(new BigDecimal(temp2)), BigDecimal.class));
    
    ValueExpression e = factory.createValueExpression(context, "${foo}", BigDecimal.class);// will return 8
    

But once again i don't really know is it right or not, so i came up with the second

  1. Create a class, make some static methods which require 2 parameters, and here it goes. Let's say this class named Operate

    public static BigDecimal add (BigDecimal val1, BigDecimal val2){
        return val1.add(val2);
    }
    
    public static BigDecimal subtract (BigDecimal val1, BigDecimal val2){
        return val1.subtract(val2);
    }
    

    Then, i call it like this

    context.setFunction("meh", "max", Operate.class.getMethod("add", BigDecimal.class, BigDecimal.class));  
    ValueExpression e = factory.createValueExpression(context, "${meh:max(fii,fee)}", BigDecimal.class);// also return 8  
    

I prefer use the second, hope this will be helpful

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=105684&siteId=1