RichFaces and standard converters

This recipe will show you how to use one of the standard converters defined in RichFaces. First you have to know that RichFaces 3.3.3 comes with a set of converters that can be found in the following packages:

  • org.richfaces.convert
  • org.richfaces.convert.rowkey
  • org.richfaces.convert.seamtext
  • org.richfaces.convert.seamtext.tags
  • org.richfaces.convert.selection
  • org.richfaces.converter

In this recipe, we will use the org.richfaces.convert.IntegerColorConverter for converting an RGB color from a RichFaces ColorPicker component into an integer and vice versa.

Getting ready

We developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library. In addition, we have used RichFaces 3.3.3.BETA1, which provides support for JSF 2.0. You can download this distribution from http://www.jboss.org/richfaces. The RichFaces libraries (including necessary dependencies) are in the book code bundle, under the |JSF_libs|RichFaces - JSF 2.0 folder.

How to do it...

In RichFaces, we can use the converter attribute or f:converter tag nested within a UIComponent. This is pretty similar to the JSF standard utilization of converters. For example, in the following code we have a colorPicker component and we apply the IntegerColorConverter converter to the selected color using the converter attribute. The result of conversion is an integer representation of the color and it is rendered into an outputText component:

<a4j:form>
<h:outputText value="The integer version of
the selected color:"/>
<h:outputText id="RGBvalue" value="#{colorPickerBean.color}"/>
<rich:panel header="RichFaces Color Picker"
style="width: 315px">
<rich:colorPicker value="#{colorPickerBean.color}"
colorMode="rgb" converter="org.richfaces.IntegerColor">
<a4j:support event="onchange" reRender="RGBvalue"/>
</rich:colorPicker>
</rich:panel>
</a4j:form>

Notice that the IntegerColorConverter ID is org.richfaces.IntegerColor. You can find the converters' IDs in the Javadoc of RichFaces.

The ColorPickerBean can be written in the following way:

package colorpicker;
public class ColorPickerBean {
private Integer color;
/**
* @return ColorPickerBean color
*/
public Integer getColor() {
return color;
}
/**
* @param ColorPickerBean color
*/
public void setColor(Integer color) {
this.color = color;
}
}

How it works...

It works exactly like a JSF standard converter. If the value passes the conversion phase, then the backing bean receives the converted value, otherwise the user gets an error message and the option to try again.

See also

The code bundled with this book contains a complete example of this recipe. The project can be opened with NetBeans 6.8 and it is named: RichFaces_standard_and_custom_converters.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset