Bravo
Bravo

Reputation: 1139

Display different names for values in Struts2 select tag

I have the following enum

public enum AccountType {
  ADMIN,
  MANAGER
}

Now, in my Struts form I have the following tag:

<s:select name="account.accountType" list="{'ADMIN', 'MANAGER'}" />

This will generate the following field.

enter image description here

Is there any way to make it generate Administrator instead of ADMIN and Manager instead of MANAGER without editing the enum on the backend?

Upvotes: 3

Views: 1756

Answers (1)

Aleksandr M
Aleksandr M

Reputation: 24396

In S2 instead of hard-coding your enum values into list in a JSP you can directly retrieve values from the enum itself using @package.Enum@values() notation. And use listValue attribute to show whatever you want based on some conditions.

For example:

<s:select name="account.accountType" list="@com.AccountType@values()"
          listValue="top == @com.AccountType@ADMIN ? 'Administrator' : top" />

Upvotes: 4

Related Questions