[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]java表达式引擎, MVEL的大概

上一篇:[转帖]java中格式化json串,美化json串
下一篇:[备忘]easyUI的grid,传form值

添加日期:2016/1/5 14:40:29 快速返回   返回列表 阅读2921次
貌似有这么几种开源引擎:Ognl/MVEL/Aviator/JSEL

大概看了看,选了MVEL

代码在此:
https://github.com/mvel/mvel

支持的表达式在此:
https://en.wikisource.org/wiki/MVEL_Language_Guide
https://en.wikibooks.org/wiki/Transwiki:MVEL_Language_Guide
很NB啊,还支持循环等语法

Maven里这么引用:


<!-- https://mvnrepository.com/artifact/org.mvel/mvel2 -->
<dependency>
    <groupId>org.mvel</groupId>
    <artifactId>mvel2</artifactId>
    <version>2.4.14.Final</version>
</dependency>



大概这么调用:


package com.why;

import java.util.HashMap;
import java.util.Map;

import org.mvel2.MVEL;

public class TestMvel {

    public static void main(String[] args) {
        String ex1 = "(a+b*3-c/200+d*2)";
        String ex2 = ex1+">0";

        Map vars = new HashMap();
        vars.put("a", new Integer(100));
        vars.put("b", new Integer(200));
        vars.put("c", new Integer(40000));
        vars.put("d", new Integer(200));

        Double result1 = (Double) MVEL.eval(ex1, vars);
        Boolean result2 = (Boolean) MVEL.eval(ex2, vars);
        System.out.println(result1.intValue());
        System.out.println(result2.booleanValue());
        
        String ex3="if (x > 0) {\r\n"
                + "   System.out.println(\"Greater than zero!\");\r\n"
                + "}\r\n"
                + "else if (x == -1) { \r\n"
                + "   System.out.println(\"Minus one!\");\r\n"
                + "}\r\n"
                + "else { \r\n"
                + "   System.out.println(\"Something else!\");\r\n"
                + "}";
        vars = new HashMap();
        vars.put("x", new Integer(2));
        MVEL.eval(ex3, vars);
        vars.put("x", new Integer(-1));
        MVEL.eval(ex3, vars);
        
        String ex4="def hello() { java.lang.Process p = java.lang.Runtime.getRuntime().exec(new String[] { \"cmd\", \"/c\", \"dir\" });"
                + "java.io.BufferedReader input = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream(),\"GBK\"));"
                + "String line = null;\r\n"
                + "            while ((line = input.readLine()) != null) {\r\n"
                + "                System.out.println(line);\r\n"
                + "            }"
                + "input.close();\r\n"
                + "     }hello();";
        MVEL.eval(ex4,vars);
    }

}

 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved