views:

1408

answers:

1

I'm using Spring for an HTML form. One of the fields is an enum and thus I'd like a HTML drop-down list (<option> tag). My enum's name is different than the toString() value. For example:

public enum Size {
    SMALL("Small"), LARGE("Large"), VERY_LARGE("Very large");

    private final String displayName;

    private Size(String displayName) {
        this.displayName = displayName;
    }

    public String toString() {
        return displayName;
    }
}

I want the user to see the toString() value. Normally this is accomplished using the itemLabel of the Spring options tag.

<form:options items="${enumValues}" itemLabel="beanProperty" />

But toString() isn't a bean property as it doesn't start with "get". Is there a way to set itemLabel to use toString's value without having to create a getter?

+1  A: 

Why not add a public getDisplayName() method to your enum?

Jeff Olson
I'm trying to avoid adding this extra method to each enum. It doesn't help that enum can't be extended.
Steve Kuo