Maven configuration:
Mons < dependency > < groupId > org.apache.com < / groupId > < artifactId > Commons - jexl3 < / artifactId > < version > 3.1 < / version > </dependency>Copy the code
public class JexlTest {
private static JexlEngine jexlEngine = new Engine();
public static Object executeExpression(String jexlExpression, Map<String, Object> map) {
JexlExpression expression = jexlEngine.createExpression(jexlExpression);
JexlContext context = new MapContext();
if (MapUtils.isNotEmpty(map)) {
map.forEach(context::set);
}
returnexpression.evaluate(context); } /** * support to call String function * @description * @author hedong * @date 2018 12月11日 10:56:49 * @modifyNote */ @test public voidtestStringFun() {
Map<String, Object> map = new HashMap<>();
map.put("num"."0123456789");
String expression = "Num. Substring (2, 6)"; //2345 System.out.println(executeExpression(expression, map)); } /** * String void judgment * @description * @author hedong * @date 2018 12月11日 11:01:27 * @modifyNote */ @test public voidtestStringEmpty() {
Map<String, Object> map = new HashMap<>();
map.put("name"."");
String expression = "(name==null||name==\"\")? \ "unknown \" : name"; // nameless system.out. println(executeExpression(expression, map)); } @Test public voidtestArray() {
Map<String, Object> map = new HashMap<>();
map.put("names", new String[] {"Zhang"."Bill"."Fifty"."Daisy"});
String expression = "names[2]"; // system.out. println(executeExpression(expression, map)); expression ="names.size()"; //4 System.out.println(executeExpression(expression, map)); }}Copy the code