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.
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}.
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With