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?
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With