- 浏览: 335185 次
- 性别:
- 来自: 重庆
文章分类
最新评论
-
hjl0722:
...
Java中的异或 -
lucd:
f(New.<Person, List<Pet&g ...
第15章泛型 -
liujunhao225:
[Error: could not access: List; ...
mvel的使用 -
superscorpio:
public void testImportInContex ...
mvel的使用 -
yuyangtina:
哦,知道了,是继承的方法。谢谢你的分享。
HttpClient3.x发送Soap请求的方法
MVEL is a powerful expression language for Java-based applications,用来计算字符串形式的表达式。
MVEL is very easy to use, and just as easy to integrate into your application. Let's take a quick look at a simple MVEL expression:
foo.name == "Mr. Foo"
This simple expression asks MVEL if the value of foo.name is equal to "Mr. Foo". Simple enough, but what exactly is foo in this case? Well, it can be at least two things:
A property of a Context Object; or An External Variable
A context object is something you can use as the base of your expression by which MVEL will attempt to map identifiers to. Consider the following example:
public class Person {
private String name;
public void setName(String name) { this.name = name; }
public String getName() { return this.name; }
}
Lets say we decide to make an instance of this particular class the context object for the expression, and we evaluate it like so:
Person personInst = new Person();
personInst.setName("Mr. Foo");
Object result = MVEL.eval("name == 'Mr. Foo'", personInst);
When we execute this expression using the eval() method, we will get a result. In this particular case, the value of result will be a Boolean true. The reason for this, is that when the expression name == 'Mr. Foo' is evaluated, MVEL looks at the context object to see if it contain a property/field called name and extracts it.If we wanted to simply extract the value of name from the person instance, we could do that as well:
String result = (String) MVEL.eval("name", personInst);
assert "Mr. Foo".equals(result);
Pretty simple stuff. But what if we want to inject a bunch of variables? MVEL supports that too, and there is both and easy way and a more advanced way (dealing with resolvers – which we won't get to here). The easy way simply involves passing in a Map of variables (names and values), like so:
Map vars = new HashMap();
vars.put("x", new Integer(5));
vars.put("y", new Integer(10));
Integer result = (Integer) MVEL.eval("x * y", vars);
assert result.intValue() == 50; // Mind the JDK 1.4 compatible code
Now, so far we've just been looking at using MVEL as a purely interpreted tool. MVEL can also compile expressions to execute them much faster using a different API. Let's convert the last expression into a compiled version:
// The compiled expression is serializable and can be cached for re-use.
CompiledExpression compiled = MVEL.compileExpression("x * y");
Map vars = new HashMap();
vars.put("x", new Integer(5));
vars.put("y", new Integer(10));
// Executes the compiled expression
Integer result = (Integer) MVEL.executeExpression(compiled, vars);
assert result.intValue() == 50;
以上代码报错,没看出问题所在,博主可否指点一下,错误如下:
[Error: could not access: List; in class: org.mvel2.ParserContext]
MVEL is very easy to use, and just as easy to integrate into your application. Let's take a quick look at a simple MVEL expression:
foo.name == "Mr. Foo"
This simple expression asks MVEL if the value of foo.name is equal to "Mr. Foo". Simple enough, but what exactly is foo in this case? Well, it can be at least two things:
A property of a Context Object; or An External Variable
A context object is something you can use as the base of your expression by which MVEL will attempt to map identifiers to. Consider the following example:
public class Person {
private String name;
public void setName(String name) { this.name = name; }
public String getName() { return this.name; }
}
Lets say we decide to make an instance of this particular class the context object for the expression, and we evaluate it like so:
Person personInst = new Person();
personInst.setName("Mr. Foo");
Object result = MVEL.eval("name == 'Mr. Foo'", personInst);
When we execute this expression using the eval() method, we will get a result. In this particular case, the value of result will be a Boolean true. The reason for this, is that when the expression name == 'Mr. Foo' is evaluated, MVEL looks at the context object to see if it contain a property/field called name and extracts it.If we wanted to simply extract the value of name from the person instance, we could do that as well:
String result = (String) MVEL.eval("name", personInst);
assert "Mr. Foo".equals(result);
Pretty simple stuff. But what if we want to inject a bunch of variables? MVEL supports that too, and there is both and easy way and a more advanced way (dealing with resolvers – which we won't get to here). The easy way simply involves passing in a Map of variables (names and values), like so:
Map vars = new HashMap();
vars.put("x", new Integer(5));
vars.put("y", new Integer(10));
Integer result = (Integer) MVEL.eval("x * y", vars);
assert result.intValue() == 50; // Mind the JDK 1.4 compatible code
Now, so far we've just been looking at using MVEL as a purely interpreted tool. MVEL can also compile expressions to execute them much faster using a different API. Let's convert the last expression into a compiled version:
// The compiled expression is serializable and can be cached for re-use.
CompiledExpression compiled = MVEL.compileExpression("x * y");
Map vars = new HashMap();
vars.put("x", new Integer(5));
vars.put("y", new Integer(10));
// Executes the compiled expression
Integer result = (Integer) MVEL.executeExpression(compiled, vars);
assert result.intValue() == 50;
评论
2 楼
liujunhao225
2014-01-13
[Error: could not access: List; in class: org.mvel2.ParserContext]
1楼,List只是个接口。你把List换成ArrayList试一下。
1楼,List只是个接口。你把List换成ArrayList试一下。
1 楼
superscorpio
2013-01-23
public void testImportInContext() { ParserContext ctx = new ParserContext(); ctx.addImport("List", List.class); ctx.addImport("ArrayList", ArrayList.class); MVEL.eval("List test = null;User u =null", ctx); }
以上代码报错,没看出问题所在,博主可否指点一下,错误如下:
[Error: could not access: List; in class: org.mvel2.ParserContext]
发表评论
-
java发邮件
2013-05-07 16:55 854发邮件需要用到mail.jar包 import ja ... -
PropertyChangeSupport的使用
2012-12-20 14:06 2688import java.beans.PropertyCh ... -
JVM运行时数据区的划分
2012-11-28 09:20 836虚拟机运行时数据区 ... -
威风威风
2012-11-20 11:26 10631. //a 不 ... -
Http基本认证
2012-10-29 15:20 972在HTTP中,基本认证是一种用来允许Web浏 ... -
如何获取美国时间
2012-10-26 10:41 2560TimeZone tz=TimeZone.getT ... -
base64加密算法简介
2012-10-23 11:32 1049什么是base64呢? 它是一种加密 ... -
比较器comparator
2012-10-11 10:46 778排序的规律跟方法的参数顺序有关。 该接口有个方法:in ... -
文件输入输出时的编码问题
2012-10-09 16:38 1037Java读取文件的 ... -
java源文件编码问题
2012-10-09 16:00 4635Java编译器在对 ... -
在java中利用ant对目录进行压缩
2012-08-31 17:05 885import java.io.File; impo ... -
File类中几个经常用到的方法
2012-07-30 16:02 2511一、File类的一些常用方法: 1.create ... -
文件读写例子
2012-07-07 10:19 964import java.io.BufferedReade ... -
字符串详解
2012-06-26 15:51 851一、字符串的六道经典题 看明白了,基本上就掌握了字符串的原理 ... -
私有成员的访问
2012-06-21 09:50 817不通过getXXX()方法获取类的私有域: public c ... -
jar文件
2012-05-31 23:41 916JAR(Java Archive,Java 归档文件) ... -
NotePad++的格式编码
2012-04-25 10:01 11954notepad默认存储文件的格式是ansi格式,如果想修改,可 ... -
字符编码概述
2012-04-24 23:59 763ASCII码 我们知道,在计 ... -
http协议的消息头
2012-04-18 17:25 2632一、HTTP消息头主要分为 ... -
HTTP协议详解
2012-04-18 17:17 935一、简介 1.http默认端口号为80,Https的端 ...
相关推荐
简单规则 结合和Java 项目演示了如何在 Java 中实现轻量级规则引擎并使决策能够表示为字符串,从而在属性文件或数据库中外部化,或通过 JMX 使用和。
mvel-maven-插件 使用 MVEL 渲染模板的 Maven 插件。 用法 < groupId>uk.co.codezen < artifactId>mvel-maven-plugin < version>1.0 < goal>render < template
NULL 博文链接:https://yanguz123.iteye.com/blog/2146176
介绍 这是一个用于测试表达式的简单 Repl(Read-Eval-Print-Loop)。...由于这是一个简单的 Maven 项目,因此可以使用 > mvn install 项目配置了Maven exec插件,直接调用即可启动repl > mvn exec:java
表过滤器排序解析器解析表达式来过滤或排序表,需要... 像这样使用它们: TableFilterTextParser . parseText(jTable, expression)TableSortTextParser . ParserText(jTable, expression , SortOrder . ASCENDING );
drools使用的jar包,运行官方drools-distribution-7.7.0.Final drools使用的jar包,运行官方drools-distribution-7.7.0.Final drools使用的jar包,运行官方drools-distribution-7.7.0.Final drools使用的jar包,运行...
telb.zip,使用ognl或mvel表达式作为组件/页参数值。tapestry5表达式语言绑定-使用ognl或mvel表达式作为组件/页参数值
antlr-runtime.jar drools-compiler.jar drools-core.jar drools-decisiontables.jar drools-jsr94.jar droolsService.jar jsr94.jar jxl.jar mvel.jar xpp3.jar
拉面规则使用 MVEL ( ) 表达式语言来定义规则条件和操作。入门以下代码段快速概述了在 Java 代码中定义一些规则的基本用法: Person john = new Person ( " John " , 15 );Person anna = new Person ( " Anna " , ...
java笔试题jsp servlet geeMVC Java 8+ MVC 框架 geeMVC 是一个为Java 8+编写的快速轻量级MVC ...它的主要重点是快速、易于使用和易于扩展。...为了简化扩展geeMVC,我们确保不存在私有或静态字段和方法,因此使其...或MVEL
我们将使用“exploit/multi/elasticsearch/script_mvel_rce”模块来利用这个漏洞(你可以使用search ElasticSearch或在Metasploit中search CVE-2014-3120找到这个模块)。 设置模块选项,检查目标是否存在漏洞,最后...
JStarCraft RNS要求使用者具有以下环境: JDK 8或以上 Maven 3 安装JStarCraft-Core框架 git clone https://github.com/HongZhaoHua/jstarcraft-core.git mvn install -Dmaven.test.skip=true 安装JStarCraft-AI...
部分命令支持mvel2表达式。如何使用1.clone代码(可选,已经发布到中央仓库,可以直接依赖中央仓库的稳定版本)git clone git@github.com:guoyang1982/woodpecker-client.git2.编译安装(可选)cd wpclient-agent/...
MVEL 和 Javascript 规则,或在 Node.js 上运行。 2.4.1 版: 更新许可证到 Apache 2.0 2.3.1 版: 删除了采用 Java 8 流的构造函数和方法,因为传递流不是一个好主意。 添加了用于使用其他变量和静态方法的构造函数...
根据您想使用版本2.xx(推荐为最新版本)还是版本1.xx,有两种不同的导入依赖项方式。 版本2.xx 可以通过以下方式将Power Flows DMN作为依赖项导入: Maven < groupId>org.powerflows < artifactId>dmn-engine ...