mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add script support
This commit is contained in:
@@ -23,7 +23,15 @@ public class JexlEngine implements ExpressionEngine {
|
||||
|
||||
@Override
|
||||
public Object eval(String expression, Map<String, Object> context) {
|
||||
return engine.createExpression(expression).evaluate(new MapContext(context));
|
||||
final MapContext mapContext = new MapContext(context);
|
||||
|
||||
try{
|
||||
return engine.createExpression(expression).evaluate(mapContext);
|
||||
} catch (Exception ignore){
|
||||
// https://gitee.com/dromara/hutool/issues/I4B70D
|
||||
// 支持脚本
|
||||
return engine.createScript(expression).execute(mapContext);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -9,6 +9,9 @@ import cn.hutool.extra.expression.engine.spel.SpELEngine;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ExpressionUtilTest {
|
||||
|
||||
@Test
|
||||
@@ -33,6 +36,17 @@ public class ExpressionUtilTest {
|
||||
Assert.assertEquals(-143.8, (double)eval, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jexlScriptTest(){
|
||||
ExpressionEngine engine = new JexlEngine();
|
||||
|
||||
String exps2="if(a>0){return 100;}";
|
||||
Map<String,Object> map2=new HashMap<>();
|
||||
map2.put("a", 1);
|
||||
Object eval1 = engine.eval(exps2, map2);
|
||||
Assert.assertEquals(100, eval1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mvelTest(){
|
||||
ExpressionEngine engine = new MvelEngine();
|
||||
|
Reference in New Issue
Block a user