Write Less, Achieve More
Download now »
 

Announcing Apache Wicket 8: Write Less, Achieve More

The Apache Wicket project announces the 8th major release of the open source Java web framework servicing websites and applications across the globe for over a decade. With this release Wicket embraces Java 8 idioms fully, allowing the use of lambda expressions in all the right places. With Wicket 8 you can write fewer, faster and more maintainable code.

A Careful Application of Java 8

While previous versions of Wicket work well with Java 8 and beyond, the Wicket API was not optimized to make full use of Java 8 idioms. After careful consideration and trying many paths to adopt new ways of combining Java 8 and Wicket, we finally settled on the current API.

Java 8’s new idioms and types have been applied throughout Wicket’s API. Learn more about the details below.

Lambdas in All The Right Places

The major feature of Java 8 was the addition of lambda support in the language. With Wicket 8 we have looked at providing lambda support in the API and have weighed it against performance and memory usage considerations.

In this light we have opted to continue using subclassing as the primary extension mechanism for Wicket components. Factories for lambdas have been provided for various forms of models (databinding) and behaviors (adapters for components).

Optional is Somtimes Required

// WICKET 7:
add(new AjaxFallbackLink<Void>("wicket7") {
    @Override
    public void onClick(AjaxRequestTarget target) {
        if(target != null) {
            // ...
        }
    }
});
// WICKET 8:
add(new AjaxFallbackLink<Void>("wicket8") {
    @Override
    public void onClick(Optional<AjaxRequestTarget> target) {
        if(target.isPresent()) {
            // ...
        }
    }
});

Models as Functional Interface

Wicket uses models as the databinding method: each data-aware component can take a model implementation to retrieve and store data in. From Wicket 8 we have made the root interface IModel a functional interface. You can now use lambdas where you can use models.

The following example binds a Label component to the name of a person object in different ways using Wicket 8:

add(new Label("name1", person::name));
add(new Label("name2", () -> person.getName()));
add(new Label("name3", LambdaModel.of(personModel, Person::getName)));
add(new Label("name4",
  LambdaModel.of(contractModel, Contract::getPerson)
    .map(Person::getName)));

The new LambdaModel type in Wicket enables type safe, refactor safe databinding for components. Previously one would bind data to components using PropertyModel, using a string expression to determine the exact property to show. The string expression is something like "person.name". This is inherently difficult to refactor when you decide to change the name field into for example "lastName". With LambdaModel, this refactoring is trivial for IDEs to perform: the Java methods can easily be renamed and the IDE will find all the references.

Microbenchmarks have shown that using lambda models provides much better performance than the string based PropertyModel at the cost of slightly larger memory footprint.