根据 MVEL 的文档,可以在脚本中导入静态 java 方法:http: //mvel.codehaus.org/Programmatic+Imports+for+2.0。以下示例取自该页面,但无法正常工作(我收到错误:无法访问属性(空父):时间)。有什么问题?
import java.io.Serializable;
import org.mvel2.MVEL;
import org.mvel2.ParserContext;
public class Test {
public static void main(String[] args) {
ParserContext ctx = new ParserContext();
try {
ctx.addImport("time", System.class.getMethod("currentTimeMillis", long.class));
}
catch (NoSuchMethodException e) {
// handle exception here.
}
Serializable s = MVEL.compileExpression("time();", ctx);
Object ans = MVEL.executeExpression(s);
System.out.println(ans.toString());
}
}