开发工具:
文件大小: 633kb
下载次数: 0
上传时间: 2010-01-17
详细说明: Getting Started for 2.0 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; Next Steps I hope that wets your appetite. Anyways, you can continue on to the Language Guide and Integration Guide for 2.0's for lots more information. You can also check out the MVEL Shell: an interactive shell. ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.