Create custum labels for enumerated types in JSF

In this article i will show you how to create custom labels for Java Enum types with a custom JSF converter. Click here to download the  example application. To run the example application you need Maven2. Simple type mvn jetty:run to start the embedded Jetty container. After a few seconds you can point your browser to http://localhost:8080/enumsample to see the application up and running.

To convert an enum type to a more friendly label, i’ve created a custom enum type converter. As you can see in the code below, that converter translate an enum value to a label in the message bundle. Based on the locale of your browser a localized messege will be returned.

package nl.jwse.sample.enumsample.converter;

import java.util.Locale;
import java.util.ResourceBundle;

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

/**
 * Generic Enum Type converter. Converts an enum value to a language specific and display-friendly
 * label. The label should be defined in a messagebundle with the following convention.
 *
 * [Full enum classname].[Enum field]=Enter displaylabel here.
 *
 * i.e.
 *
 * nl.jwse.sample.enumsample.type.Card.HEARTS=Harten
 *
 * @author Jeroen Wolsink
 *
 */
public class EnumTypeConverter implements Converter {

	@SuppressWarnings("unchecked")
	public Object getAsObject(FacesContext fc, UIComponent comp,
			String value) {
		Class enumType = comp.getValueExpression("value").getType(fc.getELContext());
		return Enum.valueOf(enumType, value);
	}

	public String getAsString(FacesContext fc, UIComponent component,
			Object object) {
		if (object == null) {
			return null;
		}
		Enum type = (Enum) object;

		return getEnumLabel(type, fc);
	}

	/**
	 * Creates a localized label for the enum type. The label must be
	 * defined in the resource bundle, or else the toString() value will
	 * bee returned.
	 * @param type EnumType
	 * @param fc FacesContext
	 * @return The label for the enumerated type
	 */
	private String getEnumLabel(Enum type, FacesContext fc) {

		String label = null;

		String messageBundle = fc.getApplication().getMessageBundle();
		Locale locale = fc.getApplication().getDefaultLocale();
		ResourceBundle rb = ResourceBundle.getBundle(messageBundle, locale);
		try {
			String key = generateKey(type);
			label = rb.getString(key);
		} catch (Exception e) {
			label = type.toString();
		}

		return label;
	}

	/**
	 * Generates a message bundle key based on an Enum Type.
	 * @param type EnumType
	 * @return Message bundle key
	 */
	private String generateKey(Enum type) {

		String className = type.getClass().getName();
		String key = className + '.' + type.toString();

		return key;
	}
}

Before you can use the converter in your JSF page, the converter must be registered in the FacesContext.xml.


	enumTypeConverter
	nl.jwse.sample.enumsample.converter.EnumTypeConverter

In the page you can add the converter to an outputText usering the converter=”enumTypeConverter” attribute. In the backingbean there is also a conversion for the Card enum type to a selectItem list. You can view the implementation in the sourcefiles of the example application




	

		
			
			

			
			
				
			

		

	

I hope this article was helpfull to you. Any comments are most welcome.

1 reactie bij Create custum labels for enumerated types in JSF

  • Tja, weer een jaartje erbij. Ik leef nog. Je had toch ff aan moeten komen. Dat bord hing er niet voor niets. Ik heb ooit gezegd dat een Rolling Stone nooit ouder wordt dan 40…….maar ik ben Still Alive! Dus die vierdaagse zal ook wel weer moeten gaan gebeuren komend jaar. Ondanks die bejaarde leeftijd.

Laat een reactie achter

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>