Java将字符串转换成可执⾏代码
1. 添加依赖
<dependency>
<groupId>org.apachemons</groupId>
<artifactId>commons-jexl3</artifactId>
<version>3.1</version>
</dependency>
2. 封装⽅法
private static JexlEngine jexlEngine = new Engine();
public static Object executeExpression(String jexlExpression, Map<String, Object> map) {
JexlExpression expression = ateExpression(jexlExpression);
JexlContext context = new MapContext();
if (Checker.isNotEmpty(map)) {
map.forEach(context::set);
}
return expression.evaluate(context);
}
3. 测试html代码转链接
@Test
public void testExecuteExpression() {
Map<String, Object> map = new HashMap<>();
map.put("alive", "coding every day");
map.put("out", System.out);
String expression = "out.print(alive)";
executeExpression(expression, map);
// uteExpression(expression, map);
}
// output:coding every day

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。