Developing Web 2.0 applications
with JavaServerFaces
and Dreamweaver
>> Just Became Easier
Documentation
Search
  1. Introduction
  2. Getting Started
  3. Developing Web Sites
  4. Using Components
  5. Internationalization
  6. Code View Support
  7. Design View Support
  8. Keyboard Shortcuts
  9. Tag Object Toolbars
  10. JSF Server Behaviors
  11. Property Inspectors
  12. JSF Tag Reference
JSF Core Tag Library
  f:actionListener
  f:ajax
  f:attribute
  f:convertDateTime
  f:convertNumber
Selected f:converter
  f:event
  f:facet
  f:loadBundle
  f:metadata
  f:param
  f:phaseListener
  f:selectItem
  f:selectItems
  f:setPropertyActionListener
  f:subview
  f:validateBean
  f:validateDoubleRange
  f:validateLength
  f:validateLongRange
  f:validateRegex
  f:validateRequired
  f:validator
  f:valueChangeListener
  f:verbatim
  f:view
  f:viewParam
If you like this documentation, please take a look at my book:

12. JSF Core Tag Reference

Documentation > JSF > JSF Tag Reference > JSF Core Tag Library > f:converter

f:converter

The Converter tag registers a Converter instance on the component associated with the enclosing tag. It takes one attribute:the converterId that you used to register your converter in your Faces configuration file. The Converter interface is implemented by classes that handle custom conversion of user input into a data type expected by the domain model of your JSF application, and vice versa. For example, you might have a select list of countries that your user can choose from. You could create a custom converter to handle converting the underlying Country objects in your list into strings to display to the user. When the user submits the form, your converter would also handle converting the selected country ID into the appropriate Country object expected by your domain model.

JSF Example

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:f="http://java.sun.com/jsf/core" 
      xmlns:h="http://java.sun.com/jsf/html">
<h:head />
<body>
<h:form>
	<h:outputLabel value="Enter your country name: " />
	<h:inputText value="#{customerController.customer.homeAddress.country}">
    	<f:converter converterId="countryConverter" />
    </h:inputText>
	<h:commandButton value="Submit" />
</h:form>
</body>
</html>
This example was formatted by JSFToolbox for Dreamweaver.

Java Code

package com.mycompany.convert;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.mycompany.controller.CountryController;
import com.mycompany.model.Country;

@FacesConverter(value = "countryConverter")
public class CountryConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		if (value == null) {
			return null;
		}
		CountryController controller = context.getApplication()
				.evaluateExpressionGet(context, "#{countryController}",
						CountryController.class);
		Country country = controller.findCountryByName(value);
		return country;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		String string = null;
		if (value instanceof Country) {
			string = ((Country) value).getName();
		}
		return string;
	}

}

Rendered Output

Rendered f:converter tag

Tag Attributes

binding Expression
 
The value-binding expression linking this component tag to a backing bean property.
converterId Text
Required
The value of the converter-id element of the converter tag in your Faces configuration file. The JSF framework will create an instance of your converter class and register it with the parent component.
for Text
Since JSF 2.0
If present, this attribute refers to the value of one of the exposed attached objects within the composite component inside of which this tag is nested.