Archives for: September 2005

Sep 30, 2005 : JSF and Java 5.0 Enums

Rich Hightower explains how he set up JSF to use Java Enums but his code can be greatly simplified.

First, instead of manually mapping keys to enum constants, you could add a key parameter to each constant which gets passed to the constructor. See Sun's Enum guide for more info. And then you could loop through MyEnumClass.values() to find what you need for the reverse mapping.

But you don't need to specify keys at all. Using .toString() and Enum.valueOf() work just fine as a key in JSF. (You'll see them used later in the converter.) Besides, putting keys in your Enum just so JSF can use them is mixing presentation and logic. Your Enum class doesn't need anything special to work with JSF.

The backing bean still needs to provide a list of choices for each select box. But we can do it with a generic method.

/** provide all choices from the ColorType enum */
public Map getColorTypeChoices() {
	Map<String,ColorType> choices = new HashMap<String,ColorType>();
	for (ColorType type : ColorType.values()) {
		choices.put(type.getLabel(), type);
	return choices;

And the converter can be improved to a single one-size-fits-all Enum converter.

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

public class EnumTypeConverter implements Converter {

	public Object getAsObject(FacesContext context, UIComponent comp,
			String value) throws ConverterException {
		Class enumType = comp.getValueBinding("value").getType(context);
		return Enum.valueOf(enumType, value);

	public String getAsString(FacesContext context, UIComponent component,
			Object object) throws ConverterException {
		if (object == null) {
			return null;
		Enum type = (Enum) object;
		return type.toString();


For each enum class, use this as the <converter-class> in faces-config.xml

The JSF tags are straightforward:

<h:selectOneListbox id="colorType" required="true" value="#{formbean.form.colorType}">
	<f:selectItems value="#{formbean.colorTypeChoices}" />

Based on all the improvements made here, I'm pretty sure they could be further simplified by creating a selectItemsFromEnum facet. More on that later, hopefully.

: Jakarta's Commons-Email vs. Spring's Mail support

Has anyone compared Commons-Email with the Spring mail abstraction layer? They seem to be very similar.

Sep 27, 2005 : If i had a million dollars...

"If I had a million dollars" is playing

Me: what if I had 8 million dollars?
Nick: you'd be full and I'd be pissed!
Me: huh?
Nick: i thought you said "ate my million dollars"

: distributed web services

An advogato article starts a discussion on free software v. free services. Clearly there is movement from locally installed software to server-provided services. Free software groups can't follow the movement the same that way for-profit groups can because they don't (easily) have funds to support service infrastructure. But the idea of distributed p2p (or whatever the latest appropriate buzzword is) services should be possible. I'm surprised we haven't seen anything like this yet. Not sure what sort of problem area it would solve though . . . hmm ... hammers and nails.

Update: just found How open source will fit in with SOA and Web services via Web Services and XML

Sep 26, 2005 : Spring Framework and Java Generics

I'd like to have a templated class instantiated by spring.

But there are no plans for java 1.5 dependencies in spring (but couldn't you add optional support for the features ...)

And moreover, it seems that generic instantiation requires the type at compile-time so it wouldn't be possible anyway.

Sep 21, 2005 : the way data should be

Census data + Google maps = super-accessible data

Sep 16, 2005 : IIS & Tomcat: same machine, same port, different IP address

Co-worker Ryan, and I, managed to get it set up without too much difficulty.

Sep 14, 2005 : ICE - In Case of Emergency

Put your "In Case of Emergency" contacts in your cell phone under "ICE" or with a name like "ICE - Dad". The ICE campaign helps emergency workers easily contact friends and family of someone who is unconcious. More info in this news article and others.

Sep 11, 2005 : fear drankie!

h2g2 last night at calvin for free

Sufjan Stevens tomorrow night (not free). He's become quite a bit more popular since last year, even getting reviews by Rolling Stones and the like. Last year's concert was great and I'm sure tomorrow's will be awesome too.


<< Previous Page

<< Previous Page

<  September 2005  >
Mon Tue Wed Thu Fri Sat Sun
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    




Syndicate this blog XML

powered by