Java Expression Language (JEXL)

JEXL是一款用于Java应用程序的轻量级表达式语言库,它支持动态脚本特性并简化了配置文件中变量及表达式的使用。该库提供了一种简单的方式让用户定义计算公式,并能在各种条件下轻松实现脚本功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Official Website

JEXL is a library intended to facilitate the implementation of dynamic and scripting features in applications and frameworks written in Java.

JEXL implements an Expression Language based on some extensions to the JSTL Expression Language supporting most of the constructs seen in shell-script or ECMAScript. 
Its goal is to expose scripting features usable by technical operatives or consultants working with enterprise platforms.

The library exposes a small footprint API - the core features fit in 3 classes and 10 methods - that can be used in various conditions:

  • Scripting features:
    • Your application lets (advanced) users evaluate or define some simple expressions like computation formulas.
  • Module or component configuration:
    • Your application has configuration files (eventually generated by a design module) consumed by the end-user module that would benefit from variables and expressions.
    • When it would be convenient to use IOC but overall complexity doesn't require (or can't depend upon) a full-blown library (Spring, Guice...).
  • Loose-coupling of interfaces and implementations or duck-typing:
    • You have optional classes that your code cant consider as compilation dependencies.
    • You have to integrate and call "legacy" code or use components that you dont want to strongly depend upon.
  • Simple template capabilities:
    • Your application has basic template requirements and JSPs or Velocity would be overkill or too inconvenient to deploy.

JEXL name stands for Java EXpression Language, a simple expression language originally inspired by Apache Velocity and the Expression Language defined in the JavaServer Pages Standard Tag Library version 1.1 (JSTL) and JavaServer Pages version 2.0 (JSP). JEXL 2.0 added features inspired by Unified EL. The syntax is now close to a mix of ECMAScript and "shell-script" making it easy to master by technical operatives or consultants. The objects exposed and their behavior obviously need to be documented though...

The API and the expression language exploit Java-beans naming patterns through introspection to expose property getters and setters. It also considers public class fields as properties and allows to invoke any accessible method.

JEXL attempts to bring some of the lessons learned by the Velocity community about expression languages in templating to a wider audience. Commons Jelly needed Velocity-ish method access, it just had to have it.

It must be noted that JEXL is not a compatible implementation of EL as defined in JSTL 1.1 (JSR-052) or JSP 2.0 (JSR-152). For a compatible implementation of these specifications, see the Commons EL project.

A Brief Example

When evaluating expressions, JEXL merges an JexlExpression or a JexlScript with a JexlContext. An Expression is created using JexlEngine#createExpression(), passing a String containing valid JEXL syntax. A simple JexlContext can be created by instantiating a MapContext; a map of variables that will be internally wrapped can be optionally provided through its constructor. The following example, takes a variable named foo, and invokes the bar() method on the property innerFoo:

    // Create or retrieve an engine
    JexlEngine jexl = new JexlBuilder().create();
    
    // Create an expression
    String jexlExp = "foo.innerFoo.bar()";
    JexlExpression e = jexl.createExpression( jexlExp );
    
    // Create a context and add data
    JexlContext jc = new MapContext();
    jc.set("foo", new Foo() );
    
    // Now evaluate the expression, getting the result
    Object o = e.evaluate(jc);

Extensions to JSTL Expression Language

While JEXL is similar to the expression language defined in JSTL, it has improved upon the syntax in a few areas:

  • Support for invocation of any accessible method (see example above).
  • Support for setting/getting any accessible public field.
  • A general new() method allowing to instantiate objects.
  • A general size() method, which works on:
    • String - returns length
    • Map - returns number of keys
    • List - returns number of elements.
  • A general empty() method, which works on Collections and Strings.
  • Support for the ternary operator 'a ? b : c' - and its GNU-C / "Elvis" variant 'a ?: c'.
  • Support for the Perl-like regex matching operators '=~' and '!~'
  • Support for the CSS3-inspired 'startsWith' and 'endsWith' operators '=^' and '=$'
  • Support for user-defined functions.
  • Misc : '+' has been overloaded to be use as a String concatenation operator

Related Resources

JEXL is not a product of the Java Community Process (JCP), but it provides a similar expression syntax. For more information about JSP 2.0 EL and JSTL 1.1 EL:

Velocity

Apache Velocity implements a similar expression language.

In particular the References section of the User Guide has some good information on properties and method which correlate directly to JEXL.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值