根据 [ http://mvel.codehaus.org/Value+Emptiness中的 MVEL 文档
empty
如果下面提到的条件为真,则应评估为真。
字符串的长度大于 0,但仅包含空格
布尔值为 false
数值为 0
但它没有给出预期的结果。
对于字符串情况,当字符串长度>0 但仅包含空格时,条件评估为假(使用的代码如下所示)。
String stringValue=" "; Map<String,Object> contextMap=new HashMap<String, Object>(); contextMap.put("stringValue", stringValue); System.out.println(MVEL.eval("stringValue == empty",contextMap));
对于布尔值,无论布尔值如何,它都会评估为假(使用的代码如下);
Boolean booleanValue=false; Map<String,Object> contextMap=new HashMap<String, Object>(); contextMap.put("booleanValue", booleanValue); System.out.println(MVEL.eval("booleanValue == empty",contextMap));
它在比较整数时显示错误。代码:
Integer integerValue=0; Map<String,Object> contextMap=new HashMap<String, Object>(); contextMap.put("integerValue", integerValue); System.out.println(MVEL.eval("integerValue == empty",contextMap));
错误:
Exception in thread "main" [Error: failed to subEval expression]
[Near : {... integerValue == empty ....}]
^
[Line: 1, Column: 17]
at org.mvel2.compiler.AbstractParser.reduce(AbstractParser.java:2653)
at org.mvel2.compiler.AbstractParser.arithmeticFunctionReduction(AbstractParser.java:2552)
at org.mvel2.MVELInterpretedRuntime.parseAndExecuteInterpreted(MVELInterpretedRuntime.java:152)
at org.mvel2.MVELInterpretedRuntime.parse(MVELInterpretedRuntime.java:49)
at org.mvel2.MVEL.eval(MVEL.java:165)
at com.Test1.main(Test1.java:15)
Caused by: java.lang.RuntimeException: cannot convert <> to a numeric type: class org.mvel2.compiler.BlankLiteral [200]
at org.mvel2.math.MathProcessor.getNumber(MathProcessor.java:702)
at org.mvel2.math.MathProcessor._doOperations(MathProcessor.java:214)
at org.mvel2.math.MathProcessor.doOperations(MathProcessor.java:79)
at org.mvel2.math.MathProcessor.doOperations(MathProcessor.java:48)
at org.mvel2.util.ExecutionStack.op(ExecutionStack.java:178)
at org.mvel2.compiler.AbstractParser.reduce(AbstractParser.java:2593)
... 5 more
为什么它不能按照文档工作?