Introduction

We are glad to introduce the new generation expression language of Java EE 7 – Expression Language 3 (EL 3) into ZK 8 so we can do more complicated and more powerful things with the newer expression language. There are many new features in EL 3 such as new operators, lambda expressions, collection operations etc. For more details, you can check the EL 3 specification, JSR-341. Next, we will go through some new features with some sample code and demo in this post.

Version: zk-8.0.0-FL-2015-02-05+

Ugrade Highlights

  • New Operators
  • Lambda Expressions
  • Collection Operators
  • Static Field and Method References

Demo

Let’s see the demo first.

Get Adobe Flash player

It’s a simple application for users to convert lengths into different units, and for selecting different display names. In this application, we only need to provide a getter and setter in the composer or MVVM just like how you would handle a POJO. The formula used to convert the units are all coded in the ZUL which EL 3 takes cares of for developers.

New Operators

In the demo’s application header, there are some greetings, the corresponding zul looks like the following

<label value="@load(('Hi, ' += vm.firstname += ' ' += vm.lastname))" />

String concatenation is one of the new operators which is very useful in helping us concatenate strings easily, but be careful to enclose EL 3 operations with parentheses.

We also support assignment and semicolon operators now, for example:

<label value="@load((incr = x -> x + 1; incr(5)))" />

It will create a new bean “incr” for the function and show 6 as label’s value.

Lambda Expressions
Each converter is implemented with lambda expressions defined in zul.

Take conversion from meter to inch as an example,

<textbox value="@load((x -> (x * 100) / 2.54)(vm.value))" 
    onOK="@command('click', key=((x -> (x * 2.54) / 100)(self.value)))" />

The syntax used is same as ones in Java SE 8 and behaves like an anonymous function which is discarded after evaluated.

We can name a lambda and evaluate indirectly like the example mentioned above.

Collection Operations

In this demo, we use collection operations to implement the name filter.

<listbox model="@load((vm.names.stream()
                               .filter(x -> x.contains(vm.filter))
                               .toList()))">

We create a string list (vm.names) as listbox’s model so we can turn collection objects into steam. These operations can then be chained together to form a pipeline.

We can also create a list by collection construction,

<label value="@load(([1, 2, 3, 4].stream().sum()))" />

The label will show 10 as the result.

Static Field and Method References

A static field or static method of a Java class can be referenced with the syntax Classname.Field, such as

<label value="@load((Math.sqrt(16)))" />

Note that java.lang.* is imported by default.

Conclusion

Now we know the capability of expression language 3, from commonly used string concatenation to complex stream operations, EL 3 can do much more than we can imagine. We can keep our composer or view model focus on the business logic while leave the trivial tasks to view. Moreover, the runtime of JVM is backward compatible to JDK 1.5, including lambda expression and collection operations.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

7 Responses to “ZK 8 x EL 3.0 and Lambda with JDK 1.5 Compatible”

  1. Mike says:

    Sorry if I didn’t get it by my own but, just for confirmation, you mean that ZK8 will require either Java EE 7 or Java 8?

  2. Tom says:

    No, ZK 8 still supports Java 5. In fact, we ported EL 3 to Java 5 to ensure ZK 8 runs correctly with Java 5 and later.

  3. Mike says:

    Thank you Tom, that’s what I understood but I didn’t want to be too optimistic 😉

  4. Cristian Daniel Ortiz Cuellar says:

    Hello could somebody explain how ZK uses lambda using Jdk 5 or 6 or 7? not only Jdk 8??

  5. Jumper Chen says:

    @Cristian, it’s all the job of the EL 3.0 engine to parse and handle the syntax of Lambda expression, not related to JDK version.

  6. typik says:

    I use zk 8.0.1 but for me using static fields and methods doesn’t work. May be I do something wrong but after trying to user last example with I got error :

  7. James Chu says:

    @typik in the last example, ZK MVVM is required.
    Please try it here: http://zkfiddle.org/sample/smcii2/1-ZK-8-x-EL-3-0

Leave a Reply