views:

43

answers:

2

I have a variable being passed to my JSP view from a spring controller that maps to an enum. It is being printed out at 'ENUM_VALUE', not very user friendly.

What is the best way to convert this to a more readable form like 'Enum value'.

I'd rather a pure EL solution so as to avoid writting more code in the controller to parse this, but all comments appreciated.

+2  A: 

That value is coming from Enum#toString() method. Just override it in your enum. E.g.

public String toString() {
    return super.toString().toLowerCase().replace("_", " ");
}

As an alternative, add a getter to the enum which does that instead. E.g. getFriendlyName(). Then you can use it in EL like ${bean.enum.friendlyName}.

BalusC
A: 

I'd add for every enum a description text when you define them. Something like this.

public enum MyEnum {

    ENUM_VALUE("your friendly enum value");

    private String description;

    //constructor
    private MyEnum(String description) {
        this.description = description;
    }

    //add a getter for description
}

your EL would look like ${yourenum.description}

Javi