预编译时 MVEL 表达式未正确评估
MVEL Expression not evaluating properly when pre-compiled
假设 a
、b
是整数,而 pets 的类型是 HashMap<String, Integer>
当我预编译下面的表达式时,CompiledExpression 对象中的 pets.containsKey(\"Dogs\")
节点为空。
CompiledExpression compiledExpression = new ExpressionCompiler("a > 0 && b > 0 && pets.containsKey(\"Dogs\")").compile();
return (boolean) MVEL.executeExpression(compiledExpression, params);
然而,当我做类似
的事情时
boolean res = (boolean) MVEL.eval("a > 0 && b > 0 && pets.containsKey(\"Dogs\")", params);
它工作得很好,我得到了适当的回复。
有什么方法可以预编译包含哈希图等对象的表达式吗?
我下面实现的都是一样的,
Map<String, String> pets = new HashMap<>();
pets.put("dog", "DOG");
pets.put("cat", "CAT");
Integer a = 10;
Integer b = 20;
Map<String, Object> params = new HashMap<>();
params.put("$a", a);
params.put("$b", b);
params.put("$map", pets);
params.put("$key", "dog");
CompiledExpression expression = new ExpressionCompiler("$a > 0 && $b > 0 && $map.containsKey($key)").compile();
System.out.println(MVEL.executeExpression(expression, params));
System.out.println(MVEL.eval("$a > 0 && $b > 0 && $map.containsKey($key)", params));
假设 a
、b
是整数,而 pets 的类型是 HashMap<String, Integer>
当我预编译下面的表达式时,CompiledExpression 对象中的 pets.containsKey(\"Dogs\")
节点为空。
CompiledExpression compiledExpression = new ExpressionCompiler("a > 0 && b > 0 && pets.containsKey(\"Dogs\")").compile();
return (boolean) MVEL.executeExpression(compiledExpression, params);
然而,当我做类似
的事情时boolean res = (boolean) MVEL.eval("a > 0 && b > 0 && pets.containsKey(\"Dogs\")", params);
它工作得很好,我得到了适当的回复。
有什么方法可以预编译包含哈希图等对象的表达式吗?
我下面实现的都是一样的,
Map<String, String> pets = new HashMap<>();
pets.put("dog", "DOG");
pets.put("cat", "CAT");
Integer a = 10;
Integer b = 20;
Map<String, Object> params = new HashMap<>();
params.put("$a", a);
params.put("$b", b);
params.put("$map", pets);
params.put("$key", "dog");
CompiledExpression expression = new ExpressionCompiler("$a > 0 && $b > 0 && $map.containsKey($key)").compile();
System.out.println(MVEL.executeExpression(expression, params));
System.out.println(MVEL.eval("$a > 0 && $b > 0 && $map.containsKey($key)", params));