if (!empty($_SERVER['HTTP_CLIENT_IP']))
$ip=$_SERVER['HTTP_CLIENT_IP'];
else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
else
$ip=$_SERVER['REMOTE_ADDR'];
?>
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.
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
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.
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,
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.
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 :
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?
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.
Thank you Tom, that’s what I understood but I didn’t want to be too optimistic 😉
Hello could somebody explain how ZK uses lambda using Jdk 5 or 6 or 7? not only Jdk 8??
@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.
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 :
@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