View source
ID Email Status Date Money
20169 dolore-labore@dolor.com LABORE 11/03/11 $ 9.009,00
49982 no-eirmod@sed.com SANCTUS 12/22/11 $ 1.676,00
90911 sanctus-dolores@diam.com ERAT 11/25/12 $ 7.542,00
95196 no-tempor@nonumy.com ALIQUYAM 01/20/13 $ 8.551,00
84423 et-sea@sadipscing.com SEA 05/31/12 $ 3.659,00
99247 eos-clita@tempor.com VOLUPTUA 12/29/12 $ 7.952,00
39711 amet-amet@At.com SEA 02/09/12 $ 4.178,00
27808 erat-vero@rebum.com GUBERGREN 04/20/13 $ 5.572,00
45493 voluptua-ipsum@eirmod.com VERO 07/05/12 $ 4.105,00
46996 invidunt-sit@justo.com ET 11/13/11 $ 2.419,00

A "decorator" is a design pattern where one object provides a layer of functionality by wrapping or "decorating" another object.

Let's assume you have list of business objects that you want to display, and the objects contain properties that don't return native Strings, and you want control over how they get displayed in the list (for example, Dates, money, numbers, etc...). It would be bad form to put this type of formatting code inside your business objects, so instead create a Decorator that formats the data according to your needs.

Notice the following 4 key things (and refer to the TableDecorator javadoc for some of the other details).

  • The Wrapper class must be a subclass of TableDecorator. There is various bootstrapping and API methods that are called inside the TableDecorator class and your class must subclass it for things to work properly (you will get a JspException if your class does not subclass it).
  • Be smart and create your formatters just once in the constructor method - performance will be a lot better...
  • Notice how the getDate() and getMoney() methods overload the return value of your business object contained in the List. They use the TableDecorator.getCurrentRowObject() method to get a handle to the underlying business object, and then format it accordingly.
  • We do not have to overload each of the other business object properties (like getID, getEmail, etc...). The decorator class is called first, but if it doesn't implement the method for the property called, then the underlying business class is called.

The way this works is that a single decorator object is created right before the table tag starts iterating through your List, before it starts processing a particular row, it gives the object for that row to the decorator, then as the various properties getXXX() methods - the decorator will be called first and if the decorator doesn't implement the given property, the method will be called on the original object in your List.

Column Decorators

You can specify decorators that work on individual columns, this would allow you to come up with data specific formatters, and just reuse them rather then coming up with a custom decorator for each table that you want to show a formatted date for. This kind of decorator must implement the ColumnDecorator interface.

ID Email Status Date
20169 dolore-labore@dolor.com LABORE 11/03/2011 19:20:55
49982 no-eirmod@sed.com SANCTUS 12/22/2011 19:20:55
90911 sanctus-dolores@diam.com ERAT 11/25/2012 19:20:55
95196 no-tempor@nonumy.com ALIQUYAM 01/20/2013 19:20:55
84423 et-sea@sadipscing.com SEA 05/31/2012 19:20:55
99247 eos-clita@tempor.com VOLUPTUA 12/29/2012 19:20:55
39711 amet-amet@At.com SEA 02/09/2012 19:20:55
27808 erat-vero@rebum.com GUBERGREN 04/20/2013 19:20:55
45493 voluptua-ipsum@eirmod.com VERO 07/05/2012 19:20:55
46996 invidunt-sit@justo.com ET 11/13/2011 19:20:55