Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EL enum string processing

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 are appreciated.

like image 875
Chris Avatar asked Dec 05 '25 12:12

Chris


2 Answers

That value is coming from Enum#name() method. Just add a getter to your enum which returns the friendly name. E.g.

public String getFriendlyName() {
    return name().toLowerCase().replace("_", " ");
}

You can use it in EL like ${bean.someEnum.friendlyName}.

like image 157
BalusC Avatar answered Dec 08 '25 01:12

BalusC


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}

like image 29
Javi Avatar answered Dec 08 '25 00:12

Javi