- 浏览: 340473 次
- 性别:
- 来自: 重庆
文章分类
最新评论
-
hjl0722:
...
Java中的异或 -
lucd:
f(New.<Person, List<Pet&g ...
第15章泛型 -
liujunhao225:
[Error: could not access: List; ...
mvel的使用 -
superscorpio:
<pre name="code" c ...
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 881发邮件需要用到mail.jar包 import ja ... -
PropertyChangeSupport的使用
2012-12-20 14:06 2743import java.beans.PropertyCh ... -
JVM运行时数据区的划分
2012-11-28 09:20 859虚拟机运行时数据区 ... -
威风威风
2012-11-20 11:26 10921. //a 不 ... -
Http基本认证
2012-10-29 15:20 1002在HTTP中,基本认证是一种用来允许Web浏 ... -
如何获取美国时间
2012-10-26 10:41 2614TimeZone tz=TimeZone.getT ... -
base64加密算法简介
2012-10-23 11:32 1083什么是base64呢? 它是一种加密 ... -
比较器comparator
2012-10-11 10:46 807排序的规律跟方法的参数顺序有关。 该接口有个方法:in ... -
文件输入输出时的编码问题
2012-10-09 16:38 1065Java读取文件的 ... -
java源文件编码问题
2012-10-09 16:00 4660Java编译器在对 ... -
在java中利用ant对目录进行压缩
2012-08-31 17:05 911import java.io.File; impo ... -
File类中几个经常用到的方法
2012-07-30 16:02 2538一、File类的一些常用方法: 1.create ... -
文件读写例子
2012-07-07 10:19 986import java.io.BufferedReade ... -
字符串详解
2012-06-26 15:51 903一、字符串的六道经典题 看明白了,基本上就掌握了字符串的原理 ... -
私有成员的访问
2012-06-21 09:50 846不通过getXXX()方法获取类的私有域: public c ... -
jar文件
2012-05-31 23:41 1021JAR(Java Archive,Java 归档文件) ... -
NotePad++的格式编码
2012-04-25 10:01 11985notepad默认存储文件的格式是ansi格式,如果想修改,可 ... -
字符编码概述
2012-04-24 23:59 798ASCII码 我们知道,在计 ... -
http协议的消息头
2012-04-18 17:25 2660一、HTTP消息头主要分为 ... -
HTTP协议详解
2012-04-18 17:17 986一、简介 1.http默认端口号为80,Https的端 ...
相关推荐
MVEL (Micro Velocity) 是一种高效、灵活且易于使用的表达式语言,主要用于处理数据和执行计算任务。MVEL 2.0 版本在继承 Java 语法的基础上进行了大量优化,以提高性能并简化开发流程。本文档旨在详细介绍 MVEL 2.0...
例如,可以使用MVEL来评估数学表达式、条件语句、循环结构以及访问和修改对象的属性。这使得MVEL成为模板引擎、配置文件或动态脚本的理想选择。 MVEL支持以下关键概念: 1. **变量和值**:你可以直接在表达式中声明...
你提到的`MVEL文档.docx`可能包含了MVEL的详细使用指南、API参考、示例代码等内容。查阅这份文档将有助于深入理解MVEL的功能和用法,解决实际开发中的问题。 总的来说,MVEL作为一种强大的表达式解析器,能够提升...
下面将详细介绍MVEL 2.0的使用方法、核心概念以及如何与应用程序集成。 ### MVEL表达式基础 MVEL表达式的例子如`foo.name == "Mr. Foo"`,这个表达式检查`foo.name`的值是否等于字符串"Mr. Foo"。这里的`foo`可以...
使用 Easy Rules 和 MVEL 的好处在于,它们可以帮助开发者将业务逻辑从核心应用程序代码中解耦,使其更易于理解和维护。此外,由于规则可以在运行时动态加载和修改,这使得系统能够快速适应业务需求的变化,提高了...
mvel-maven-插件 使用 MVEL 渲染模板的 Maven 插件。 用法 < groupId>uk.co.codezen < artifactId>mvel-maven-plugin < version>1.0 < goal>render < template
使用这个类,你需要将对象放入一个Map中,然后将Map与模板合并。在模板中,你可以通过键来引用Map中的对象。 **变量注入** 除了通过对象访问属性外,MVEL还支持直接注入变量。你可以通过Map传递一组变量及其值: ...
介绍 这是一个用于测试表达式的简单 Repl(Read-Eval-Print-Loop)。...由于这是一个简单的 Maven 项目,因此可以使用 > mvn install 项目配置了Maven exec插件,直接调用即可启动repl > mvn exec:java
`mvel-jsr223`项目则是MVEL对JSR 223规范的支持和实现,意味着你可以将MVEL作为一种符合JSR 223的脚本引擎在Java应用程序中使用。通过这个扩展,开发者可以利用MVEL的强大功能,同时享受到JSR 223带来的便利,比如与...
表过滤器排序解析器解析表达式来过滤或排序表,需要... 像这样使用它们: TableFilterTextParser . parseText(jTable, expression)TableSortTextParser . ParserText(jTable, expression , SortOrder . ASCENDING );
同时,为了处理复杂的表达式,可能需要实现栈数据结构来辅助运算符的优先级处理,或者使用递归下降解析(Recursive Descent Parsing)技术。 此外,对于更复杂的表达式,例如包含函数调用、变量引用和条件判断,...
在这个项目中,开发者使用了OGNL(Object-Graph Navigation Language)和MVEL(Mvel Expression Language)这两种强大的表达式语言作为组件和页面参数的值绑定方式。 GrooScript是一种动态脚本语言,它旨在简化Java...
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包,运行...
- **插件式语言支持**:允许使用不同的脚本语言,特别是全面支持MVEL脚本语言。 - **DSL引擎重构**:全新的DSL引擎支持本地化,提高了定制性和适应性。 - **属性变换支持**:Fact属性可以在返回值约束和内嵌求值时...
- **修复源码**:针对`mvel-2.0.9`版本中存在的问题,团队对其源代码进行了修改,确保不再使用的对象能够被正确清理。 - **升级版本**:考虑到问题出现在特定版本中,考虑升级到更高版本的MVEL库,以避免已知的问题...