Friday, December 17, 2010

Retrofitting a UI: GatewayWarDeployment example

Adam Bien recently asked me to create a Metawidget example for his Java EE Patterns and Best Practices Kenai Repository, based on his Real World Java EE Patterns book.

In order to demonstrate the changes typically required to incorporate Metawidget into a project (and the code savings it typically brings) I retrofitted one of Adam's original projects, his GatewayWarDeployment example. I've called mine GatewayWarDeploymentWithMetawidget.

To view the new example:
  1. Ensure you have Metawidget installed at its default location (/metawidget-1.05/metawidget.jar)

  2. Get sources for the Java EE Patterns Kenai project in NetBeans using Team > Team Server > Get Sources... (see this screencast for detailed instructions)
  3. Open the GatewayWarDeploymentWithMetawidget project
The example works just as before, but two files have been changed. The first is the managed bean,, which now includes Metawidget annotations and is therefore longer. The second is the UI page, createorder.xhtml, which now uses a <m:metawidget> tag and is therefore shorter:

In such a small example there is no overall saving in lines of code (larger examples have more dramatic savings). But there is still a significant reduction in duplicated declarations (field names, types, action names etc) and therefore reduced possibility of error when building (and maintaining) the UI. Note Metawidget is handling both the input boxes and the buttons, including enabling/disabling the buttons in line with the business logic. This is declared by the code:

@UiFacesAttribute( name = "read-only", expression = "#{load.emptyLoad}" )
public void dropLightest(){

Of course, this is not a great demonstration of all the features of Metawidget. More extensive examples can be found under the /examples folder of the Metawidget distribution. But hopefully it's enough to get people excited without confusing them with lots of code.

My thanks to Adam for his help and encouragement in developing this example!


lucho said...

example dont work please fix

@UiFacesAttribute( name = "read-only", expression = "#{load.emptyLoad}" )

lost in metawiget-all.jar

Richard said...

Apologies. As of Metawidget 1.30...'ll need to do:

@UiAttribute( name = "read-only", value = "#{load.emptyLoad}" )

Can you try that?



lucho said...

thank you very much i will try