• Fat Cats Boardgame
  • Wicket UI Library
  • About Me

Java and Wicket web development thoughts, tutorials, and tips

roman@coderdreams.com
Coder DreamsCoder Dreams
  • Fat Cats Boardgame
  • Wicket UI Library
  • About Me

How to replace PropertyModel with Wicket 8 LambdaModel

August 23, 2019 Posted by Roman Sery models, wicket No Comments

Why PropertyModel’s are used

Before Wicket 8, and before Java 8 introduced lambda expressions, you were pretty much forced to use the PropertyModel. Through the magic of reflection, PropertyModel is a very compact and simple way to specify your models for fields without having to write verbose code.

Let’s look at a simple example for a TextField that captures a user’s name, and a ListMultipleChoice for capturing their favorite movie genres. You might do it this way using PropertyModel:

new TextField<String>("displayName", new PropertyModel<String>(user, "displayName"));
new ListMultipleChoice<String>("favGenres", new PropertyModel<List<String>>(user, "favGenres"),
                new ListModel<>(List.of("Action","Comedy","Romance","Sci-fi")));

The problem with PropertyModel

The above code is very simple and compact, but it’s difficult to maintain, error prone, and can’t take advantage of modern IDE refactoring tools because it relies on strings.

Imagine you later change the displayName field of User to something else. The IDE won’t be able to automatically change the expression “displayName” passed to the PropertyModel, so unless you remember to change the expressions everywhere, you will have introduced a bug that can only be discovered at runtime.

Modern IDE’s also provide great tools for searching for references, which is very helpful when dealing with large code bases. If you were to search for all references of User.getDisplayName(), the IDE would not be able to see that PropertyModel reference.

You can solve pretty much all of these problems by replacing each instance of PropertyModel with custom implementations of Model, but that would lead to very verbose code. There is a better way!

Using LambdaModel

Using Wicket 8 LambdaModel‘s, we can keep all the benefits IDE’s offer us and maintain code compactness. First, we need to define a few helper functions that we will put into an interface that WebPage‘s will implement:

default <T extends Serializable> IModel<T> objModel(SerializableSupplier<T> getter, SerializableConsumer<T> setter) {
        return LambdaModel.of(getter, setter);
    }
default <T extends Serializable> IModel<List<T>> objListModel(SerializableSupplier<List<T>> getter, SerializableConsumer<List<T>> setter) {
        return LambdaModel.of(getter, setter);
    }

We use these helper functions to make our code even more compact and to not directly rely on LambdaModel in case we later want to switch to some other model or an even better implementation introduced in Wicket 10 馃檪

But essentially, they are a shortcut for instantiating a LambdaModel, given a getter and setter method. The first method operates on single values, while the second method operates on List‘s.

We can now update our Wicket fields to use the new models. As you can see the code is almost the same and just as compact:

new TextField<String>("displayName", objModel(user::getDisplayName, user::setDisplayName));
new ListMultipleChoice<String>("favGenres", objListModel(user::getFavGenres, user::setFavGenres), new ListModel<>(List.of("Action","Comedy","Romance","Sci-fi")));

I think PropertyModel’s were a necessary evil in previous versions of Wicket, but now that better alternatives exist, you should definitely replace them.

In general, it’s a great idea to create helper methods like the above, to isolate the usage of specific Model implementations. This makes it much simpler to make changes in large code bases as new versions of Java/Wicket continuously get released.

Check out the full working sample here!

No Comments
Share
1

About Roman Sery

I've been a software developer for over 10 years and still loving Java!

You also might be interested in

Tracking down a bug in production Wicket application

Jun 20, 2020

Investigating a problem with duplicate markup ID's generated by Wicket and how to solve it.

Understanding Wicket Models

Oct 5, 2019

The most important part of being an effective Wicket developer is understanding how to use Models. The #1 rule is, always use Models! Here's how.

How to catch uncaught exceptions in Wicket

Aug 1, 2019

Log and report all uncaught exceptions thrown by Wicket and make your developers/testers happy!

Categories

  • aws
  • customization
  • database
  • debugging
  • enum
  • java
  • models
  • performance
  • projects
  • react
  • software design
  • Spring
  • tool
  • Uncategorized
  • wicket

Recent Posts

  • Rent Day
  • Self-contained Wicket Fragments
  • Pros and cons of unit testing
  • Themeable React Monopoly board
  • Please dont use client-specific release branches

Recent Comments

  • TCI Express Thanks for sharing such insightful information. TCI Express truly stands out as the best air logistics company, offering fast, secure, and efficient air express and cold chain transportation services....

    Tracking down a bug in production Wicket application 路  March 25, 2025

  • Tom Error: A zip file cannot include itself Can you please correct the plugin part so it doesn't use the same folder as input?

    Deploying Spring Boot app to AWS Beanstalk with Nginx customization 路  September 3, 2021

  • Golfman: Reality always wins I've used both Wicket and front-end JS frameworks and, having worked extensively on both, I can tell you that "Speed of development" is definitely NOT with the JS frameworks. You basically end up...

    Five reasons you should use Apache Wicket 路  August 29, 2021

  • Kiriller Sorry can not agree with you, wicket might be a well built technical framework. But the advantages of using a front-end framework like react.js and vue.js can not be beaten by Wicket nowadays. - Speed...

    Five reasons you should use Apache Wicket 路  August 23, 2021

  • Bernd Lauert Sorry but i have to refute your claims with the following arguments: 1. the Wicket community may be small but it is also very responsive, you usually get a helpful answer from the core devs on the...

    Five reasons you should use Apache Wicket 路  July 1, 2021

Archives

  • May 2021
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019

Contact Me

Send Message
Prev Next