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 = MVELpileExpression("x * y"variable used in lambda);
Map vars = new HashMap();
vars.put("x", new Integer(5));
vars.put("y", new Integer(10));
// Executes the compiled expression
Integer result = (Integer) uteExpression(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.
Language Guide for 2.0
MVEL has largely been inspired by Java syntax, but has some fundamental differences aimed at making it more efficient as an expression language, such as operators that directly support collection, array and string matching, as well as regular expressions.
In addition to the expression language, MVEL serves as a templating language for configuration and string construction.
MVEL 2.x expressions may consist of:
Property expressions
Boolean expressions
Method invocations
Variable assignments
Function definitions
Contents
1 Basic Syntax
2 Operators
3 Value Tests
4 Inline List, Maps and Arrays
5 Property Navigation
6 Literal Values
7 Type Literals
8 Control Flow
9 Projections and Folds
10 Assignments
11 Function Definition
12 Lambda Expressions
13 Macros
14 Interceptors
15 Typing
16 Shell
17 Language FAQ
Extras
18 Sample Scripts
Still Stuck? CoreConfidenceTests is a good place to look for examples on all of MVEL's language capabilities.
Basic Syntax
MVEL is an expression language based on Java-syntax, with some marked differences specific to MVEL.  Unlike Java however, MVEL is dynamically typed (with optional typing),
meaning type qualification is not required in the the source.
An MVEL expression can be as simple as a single identifier, or as complicated as a full blown boolean expression with method calls and inline collection creations.
Simple Property Expression
user.name
In this expression, we simply have a single identifier (user.name), which by itself, is what we refer to in MVEL as a a property expression, in that the only purpose of the expression is to extract a property out of a variable or context object.  Property expressions are one of the most common uses, allowing MVEL to be used as a very high performance, easy to use, reflection-optimizer.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。