简体   繁体   English

如何在 MVEL 表达式中使用变量?

[英]How to use variables inside an MVEL expression?

I have pre-defined variables in my Java code which I want to use inside an MVEL expression.我的 Java 代码中有预定义的变量,我想在 MVEL 表达式中使用这些变量。 I don't want to pass a context.我不想传递上下文。

String Col1 = "C";
String Col2 = "D";
String expression = "Col1 == 'C' && Col2 == 'D'";

Boolean result = (Boolean) MVEL.eval(expression);

How do I read the variable values and evaluate the expression to true or false?如何读取变量值并将表达式评估为真或假?

You need to add your variables, col1 and col2 to a context object and then pass this object to MVEL.eval .您需要将变量col1col2添加到上下文 object 中,然后将此 object 传递给MVEL.eval Given below is the working example:下面给出的是工作示例:

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

import org.mvel2.MVEL;

public class Test {
    public static void main(String[] args) {
        Map<String, Object> context = new HashMap<String, Object>();

        String col1 = "C";
        String col2 = "D";

        context.put("col1", col1);
        context.put("col2", col2);

        String expression = "col1 == 'C' && col2 == 'D'";
        Boolean result = (Boolean) MVEL.eval(expression,context);
        System.out.println(result);//true

        expression = "col1 == 'E' && col2 == 'D'";
        result = (Boolean) MVEL.eval(expression,context);
        System.out.println(result);//false
    }
}

Feel free to let me know in case you have any further doubt.如果您有任何进一步的疑问,请随时告诉我。

Update: the following update is to explain why you need the context object (you have mentioned in your comment that you do not want to add your variables to a context object).更新:以下更新是为了解释为什么您需要上下文 object(您在评论中提到您不想将变量添加到上下文对象)。

If you look into the documentation at https://github.com/mvel/mvel/blob/master/src/main/java/org/mvel2/MVEL.java , you will be tempted to use the following method:如果您查看https://github.com/mvel/mvel/blob/master/src/main/java/org/mvel2/MVEL.java的文档,您会很想使用以下方法:

public static Object eval(String expression) {
    return new MVELInterpretedRuntime(expression, new ImmutableDefaultFactory()).parse();
}

However, the following piece of code will fail to compile:但是,以下代码将无法编译:

String col1 = "C";
String col2 = "D";
String expression = "col1 == 'C' && col2 == 'D'";
System.out.println(new MVELInterpretedRuntime(expression, new ImmutableDefaultFactory()).parse());

The reason for this is, the visibility of the following constructor is not public .这样做的原因是,以下构造函数的可见性不是public

MVELInterpretedRuntime(String expression, VariableResolverFactory resolverFactory) {
    setExpression(expression);
    this.variableFactory = resolverFactory;
}

So, you need to populate a context object in your client program and pass this object, along with the expression, to the program/method evaluating MVEL expression.因此,您需要在客户端程序中填充上下文 object 并将此 object 与表达式一起传递给评估 MVEL 表达式的程序/方法。 In my program, it is the main method where I am populating the context object as well as evaluating the MVEL expression.在我的程序中,这是我填充上下文 object 以及评估 MVEL 表达式的main方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM