Using Lambda expressions with JSF 2.2

JSF 2.2 comes in conjunction with EL 3.0. This version of the expression language offers a rarely known highlight: It is capable of using lambda expressions and streams. Even thought defined withing the Jave EE 7 spec (and not Java 8), you can still use these cool features!

Now, let’s build up a simple example:

A car dealer offers a bunch of cars. These cars will be listed on his web site.

Here are the simplified classes. Don’t care about the sense – it’s just for demo purpose. (Package and imports are omitted for brevity.)

public class Car {
    private final String _name;
    private final String _color;

    public Car(String name, String color) {
        _name = name;
        _color = color;
    }

    public String getName() {
        return _name;
    }

    public String getColor() {
        return _color;
    }
}
public class Dealer {

    private final String _name;
    private final List<Car> _cars = new ArrayList<>();

    public Dealer(String name) {
        _name = name;
    }

    public String getName() {
        return _name;
    }

    public List<Car> getCars() {
        return _cars;
    }
}

These classes are populated to JSF by a simple bean:

@Named
@RequestScoped
public class DemoBean {

    private final Dealer _dealer = new Dealer("NoName");

    public DemoBean() {
        initCars();
    }

    public Dealer getDealer() {
        return _dealer;
    }

    private void initCars() {
        _dealer.getCars().add(new Car("VW", "red"));
        _dealer.getCars().add(new Car("Ford", "green"));
        _dealer.getCars().add(new Car("Chevrolet", "red"));
        _dealer.getCars().add(new Car("BMW", "blue"));
        _dealer.getCars().add(new Car("Ferrari", "red"));
        _dealer.getCars().add(new Car("Mercedes", "green"));
        _dealer.getCars().add(new Car("Mercedes", "blue"));
    }
}

Last, but not least, all cars of this dealer are displayed in a h:dataTable:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Lambda demo</title>
    </h:head>
    <h:body>
        <h1>Lambda demo</h1>
        <h:dataTable value="#{demoBean.dealer.cars}" var="car">
            <h:column>
                <f:facet name="header">
                    <h:outputLabel value="Trademark"/>
                </f:facet>
                <h:outputText value="#{car.name}"/>
            </h:column>
            <h:column>
                <f:facet name="header">
                    <h:outputLabel value="Color"/>
                </f:facet>
                <h:outputText value="#{car.color}"/>
            </h:column>
        </h:dataTable>
    </h:body>
</html>

In this approach, all cars are displayed. Now, we want to display the blue cars only. A traditional approach is to modify the backing bean: Add a getter which offers a list of blue cars. But, using lambdas, there is a more elegant way. This feature enables us to add a filter directly to the web page.

Simply replace

<h:dataTable value="#{demoBean.dealer.cars}" var="car">

by

<h:dataTable value="#{demoBean.dealer.cars.stream().filter(car->car.color.equals('blue')).toList()}" var="car">

What happened?

Our list of cars is transformed into a stream. We apply a filter to this stream using a lambda expression (indicated by “->”). This means: For every element of the stream (which are cars), perform a boolean test. If true, take that element. Finally, a list is created by all elements according to that filter.

That’s it.

In a real world scenario, you would not compare to a hard-coded string, but variable content. For example, let the user choose his favorite color.

If you develop this app using NetBeans, you may try the version without lambdas first. NetBeans offers you a perfect autocomplete for the variable “car”. Once, you are ready, you add the stream. Using the filtered list, NetBeans is not able to offer autocomplete for the looping var. I’m shure, this get enhanced some day… ;-)

 

 

 

 

 

This entry was posted in NetBeans, Web development and tagged , , . Bookmark the permalink.

2 Responses to Using Lambda expressions with JSF 2.2

  1. Welyab Paula says:

    Hi.
    It’s need Java 8 to use EL lambda features ?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>