http://mvel.documentnode.com/#language-guide-for-2.0
GitHub - mvel/mvel: MVEL (MVFLEX Expression Language)
1 expresstion
var == empty
var == null
Strict Typing
Strict typing in MVEL is an optional mode for the compiler, in which all types must be fully qualified, either by declaration or inference.
Enabling Strict Mode
When compiling an expression, the compiler can be put into strict mode through the
ParserContextby settingsetStrictTypeEnforcement(true).Satisfying type strictness can be accomplished both by requiring explicit declaration of types inside the expression, or by notifying the parser ahead of time of what the types of certain inputs are.
For example:
ExpressionCompiler compiler = new ExpressionCompiler(expr); ParserContext context = new ParserContext(); context.setStrictTypeEnforcement(true); context.addInput("message", Message.class); context.addInput("person", Person.class); compiler.compile(context);In this example we inform the compiler that this expression will be accepting two external inputs:
messageandpersonand what the types of those inputs are. This allows the compiler to determine if a particular call is safe at compile time, instead of failing at runtime.
2 template
MVEL 2.0 Template Integration
Using MVEL templates is straight-forward and easy. Like regular MVEL expressions, they can be executed interpretively, or be pre-compiled and be re-used for faster evaluation.
The
org.mvel.templates.TemplateRuntimeClassThe
TemplateRuntimeclass is the center of the template engine. You can pass a template to be evaluated to the template engine by way of theeval()method.In general, the template engine follows all the same rules for context and variable binding, with an overloaded set of
eval()methods.Here’s a simple example of parsing a template interpretively:
String template = "Hello, my name is @{name.toUpperCase()}"); Map vars = new HashMap(); vars.put("name", "Michael"); String output = (String) TemplateRuntime.eval(template, vars);At the end of execution, the “output” variable will contain the string:
Hello, my name is MICHAELThe
org.mvel.templates.TemplateCompilerClassThe
TemplateCompilerclass allows for pre-compilation of the templates.When you compile a template, a compact, reusable evaluation tree is produced that can be quickly used to evaluate a template. It is used straightforwardly:
String template = "1 + 1 = @{1+1}"; // compile the template CompiledTemplate compiled = TemplateCompiler.compileTemplate(template); // execute the template String output = (String) TemplateRuntime.execute(compiled);At the end of execution, the “output” variable will contain the string:
1 + 1 = 2
MVEL2.0提供了表达式和模板的功能。在严格模式下,编译器要求所有类型都必须明确指定。启用严格模式可以通过设置`ParserContext`的`setStrictTypeEnforcement(true)`。模板引擎的核心是`TemplateRuntime`类,用于解释执行模板,而`TemplateCompiler`则用于预编译模板以提高效率。此外,文章还提到了MVEL的调试方法。
3709

被折叠的 条评论
为什么被折叠?



