Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does anyone have any simple JEXL examples using a loop. I am looking to iterate around a simple Array to output various string values?

Tags:

java

jexl

Does anyone have any simple JEXL examples using a loop. I am looking to iterate around a simple object arraylist to output various string values?

like image 272
user1816880 Avatar asked Dec 27 '22 14:12

user1816880


2 Answers

A full example with input for 452' is here :

public static void testSimpleList() {

        List<String> list = new ArrayList<String>();
        list.add("one");
        list.add("two");


        JexlContext jexlContext = new MapContext();
        jexlContext.set("list", list);;

        Map<String, Object> functions1 = new HashMap<String, Object>();
        functions1.put("system", System.out);


        JexlEngine jexl = new JexlEngine();
        jexl.setFunctions(functions1);
        Expression expression = jexl.createExpression("for(item : list) { system:println(item) }");


        expression.evaluate(jexlContext);


    }

Output :

one
two
like image 128
Balaji Boggaram Ramanarayan Avatar answered Jan 18 '23 23:01

Balaji Boggaram Ramanarayan


Looks like it requires one to use script instead of expression.

This fails with error "parsing error in 'for'"

e = new org.apache.commons.jexl3.JexlBuilder().create();
c = new org.apache.commons.jexl3.MapContext();
c.set("a", Arrays.asList(1,2,3));
e.createExpression("for(x : a) { b=x }").evaluate(c)

This, however, works

e.createScript("for(x : a) { b=x }").evaluate(c)
like image 32
mvmn Avatar answered Jan 19 '23 00:01

mvmn