Thursday, March 18, 2010

Form Generation: now with improved Conditional Attributes

The next release of Metawidget (v0.95) includes a small but useful improvment to JSF conditional attributes.

Previously you could make attributes of your UI 'conditional' by annotating them with UiFacesAttribute. For example:

public class Penguin {
   .
   .
   .
   @UiFacesAttribute( name = HIDDEN, expression = "#{empty penguin.current.condition}" )
   @UiComesAfter( "dateOfBirth" )
   public PenguinCondition getCondition() {
      ...
   }
}

This would hide the condition property if it was empty. In order words, if the JSF EL expression #{empty penguin.current.condition} evaluated to true.

The problem with this code is that, although the annotation is placed on the business object, the EL expression #{penguin.current.condition} relies on the JSF context being properly initialized with a penguin managed bean. This is rather brittle.

As a solution, in v0.95 FacesInspector can inject a this attribute 'just in time' into the FacesContext. So now you can do...

public class Penguin {
   .
   .
   .
   @UiFacesAttribute( name = HIDDEN, expression = "#{empty this.condition}" )
   @UiComesAfter( "dateOfBirth" )
   public PenguinCondition getCondition() {
      ...
   }
}

...and condition will be hidden regardless of how the external JSF environment is configured.

Feedback welcome!

0 comments: