java 有没有人有任何使用循环的简单 JEXL 示例。我希望围绕一个简单的数组进行迭代以输出各种字符串值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13336761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:19:42  来源:igfitidea点击:

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

javajexl

提问by user1816880

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?

有没有人有任何使用循环的简单 JEXL 示例。我希望围绕一个简单的对象数组列表进行迭代以输出各种字符串值?

回答by Balaji Boggaram Ramanarayan

A full example with input for 452' is here :

输入 452' 的完整示例如下:

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);


    }
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
one
two

回答by mvmn

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

看起来它需要一个使用脚本而不是表达式。

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

这失败并出现错误“'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)

回答by 452

Loop through items of an Array, Collection, Map, Iterator or Enumeration, e.g.

循环遍历数组、集合、映射、迭代器或枚举的项目,例如

for(item : list) {
    x = x + item; 
}

Where item and list are variables. The JEXL 1.1 syntax using foreach(item in list) is now deprecated.

其中 item 和 list 是变量。使用 foreach(item in list) 的 JEXL 1.1 语法现已弃用。

http://commons.apache.org/proper/commons-jexl/reference/syntax.html

http://commons.apache.org/proper/commons-jexl/reference/syntax.html

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.jexl2.Expression;
import org.apache.commons.jexl2.JexlContext;
import org.apache.commons.jexl2.JexlEngine;
import org.apache.commons.jexl2.MapContext;

public class Main {

    public static void main(String[] args) {
        JexlContext jexlContext = new MapContext();
        Map<String, Object> functions = new HashMap<String, Object>();
        functions.put("system", System.out);
        JexlEngine jexl = new JexlEngine();
        jexl.setFunctions(functions);
        Expression expression = jexl.createExpression("for(item : list) { system:println(item) }");
        expression.evaluate(jexlContext);
    }

}

pom.xml

pom.xml

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-jexl</artifactId>
    <version>2.1.1</version>
</dependency>

回答by Pratik

First and foremost thing, you should use Script evaluator, instead of Expression evaluator. Expression evaluators do not support complex looping syntax. Refer here

首先,您应该使用Script evaluator,而不是Expression evaluator。表达式求值器不支持复杂的循环语法。参考这里

JexlScript

These allow you to use multiple statements and you can use variable assignments, loops, calculations, etc. More or less what can be achieved in Shell or JavaScript at its basic level. The result from the last command is returned from the script.

JxltEngine.Expression

These are ideal to produce "one-liner" text, like a 'toString()' on steroids. To get a calculation you use the EL-like syntax as in ${someVariable}. The expression that goes between the brackets behaves like a JexlScript, not an expression. You can use semi-colons to execute multiple commands and the result from the last command is returned from the script. You also have the ability to use a 2-pass evaluation using the #{someScript} syntax.

脚本

这些允许您使用多个语句,您可以使用变量赋值、循环、计算等。或多或少可以在基本级别的 Shell 或 JavaScript 中实现。从脚本返回最后一个命令的结果。

JxltEngine.Expression

这些是生成“单行”文本的理想选择,例如类固醇上的“toString()”。要进行计算,您可以使用 ${someVariable} 中的类似 EL 的语法。括号之间的表达式的行为类似于 JexlScript,而不是表达式。您可以使用分号来执行多个命令,最后一个命令的结果从脚本中返回。您还可以使用 #{someScript} 语法使用 2-pass 评估。

Try this instead,

试试这个,

JexlScript jexlExpression = jexl.createScript("var x='';for(item:[1,2,3]){ x=x+item;}");

jexlExpression.evaluate(jexlContext);

Once this will work for you, you can use your own Array as well.

一旦这对您有用,您也可以使用自己的数组。