• 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

Reducing Wicket page size part 2: AttributeModifier and AttributeAppender

September 22, 2019 Posted by Roman Sery performance, wicket No Comments

You read part 1, right? If you didn’t, start there first and come back!

So now that you know how important it is to reduce Wicket page size, let’s talk about the AttributeModifier/AttributeAppender. They are convenience behaviors that can be added to any Component in order to dynamically add or modify markup attributes such as class, style, etc.

A simple example

We can show a simple use-case of an AttributeModifier. Suppose you are using a Label to display a percentage in some sort of financial application. You want to change the background color to green in the case of a positive percentage, and red for negative. You might think of doing it this way using an AttributeModifier:

public class ProfitLabel extends Label {
        ProfitLabel(String id, IModel<Double> model) {
            super(id, model);
            add(new AttributeModifier("style", model.getObject() > 0 ? "background-color-green" : "background-color-red"));
        }        
    }

The problem is that everywhere you use this Label, you are also creating an additional behavior which increases the size of the Wicket page. We can replace with a different implementation.

Using onComponentTag()

We can achieve the same effect by overriding onComponentTag() instead of creating a Behavior:

public class ProfitLabel extends Label {
        ProfitLabel(String id, IModel<Double> model) {
            super(id, model);            
        }
        @Override
        protected void onComponentTag(final ComponentTag tag) {
            super.onComponentTag(tag);
            Double percent = (Double) getDefaultModelObject();
            tag.put("style", percent > 0 ? "background-color-green" : "background-color-red");
        }
    }

Testing the impact of AttributeModifiers/Appenders

We can again create a test page to observe the difference in page size. The page will have a ListView that renders 100 Label’s each with a random background color. You can find the full source code on Github.

Results

Using AttributeModifier: 78 KB

Using onComponentTag: 50 KB

That’s a pretty big difference! As we have seen, Wicket provides us many convenient behaviors and components, but we must think carefully before using them. Ultimately, using a couple Behaviors here or there won’t matter much, but when you are dealing with highly reused code, think about how much they are contributing to the page size.

Use the debug bar to frequently keep an eye on the page size as well. It’s much easier to catch things sooner, rather than later. I hope you enjoy these posts about Wicket page size and would love to hear other ideas about reducing it!

No Comments
Share
4

About Roman Sery

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

You also might be interested in

Customizing your Wicket codebase: Part 3 – Custom Text

May 16, 2020

Another powerful technique for customizing using property files.

Customizing your codebase for each customer using Enumerations

Jul 26, 2019

Use enums and properties files to define all the behaviors of your application that are available for customization to your clients.

Using MySQL JSON columns to simplify your data storage: Part 1

Nov 28, 2019

Simplify data storage in your apps by using JSON column types instead of relying on database normalization.

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