Using Enum Types In Struts 2
Introduction
It is sometimes useful to use an enum type as a field in a model class or as a field in a Struts 2 ActionSupport class. This article discusses how to use enumerations with the Struts 2 framework and provides a complete example application to demonstrate the use of enumerations in a Struts 2 application.
Example Application
Download the example application that uses enum types in its model and action classes. The example application is a standard Maven project created using the Eclipse 3.6 Java IDE.
You can import this project into Eclipse by using Eclipse's File - Import feature. In the project's root folder is a README.txt file that explains how to build and run the application. If you're not using Eclipse with the Maven plugin, you can unzip the archived download and view the source code in any text editor. If you are using a Java IDE that enables importing of Maven projects you should be able to import the unzipped project.
Using Enums With Struts 2
The Struts 2 framework provides support for the automatic conversion of a String to its associated enum type field and from the enum type field to its String representation. For example if you had this enum class:
public enum Gender {
MALE,FEMALE,UNKNOWN ;
}
and this field in your action class with its appropriate public set/get methods
private Gender gender ;
Then this code in the view class
<s:property value="gender" />
would render either the String MALE, FEMALE, or UNKNOWN depending on gender's value.
If you wanted to create a set of radio buttons with choices of MALE, FEMALE, and UNKNOWN you would need this code in your Action class (with its associated public set/get methods).
private Gender[] genders = Gender.values();
Then in the view page you could use this Struts 2 form tag to create set of radio buttons.
<s:radio key="gender" list="genders" />
This would render as
Controlling What Is Displayed For The Enum Type In Struts 2
The Struts 2 enum converter class calls the toString method of the Enum class and displays the String returned. By default an enum will return the field as a String; so the enum field MALE will return the String "MALE". If you want a different String to display then you must override the toString method in your enum class and designate what String value should be returned.
In the example application is an enum named PayPeriod. In this enum I've overridden the toString method so that it returns
a different String value. If the enum field is ONEWEEK, the toString will return "Once a week". So the radio list in the
view page will look like
Be sure to examine the setPayPeriod method in the EditAction class to see how the String submitted by the form is converted to the correct PayPeriod enum field.
Summary
Struts 2 provides good support for automatically converting from an enum field to a String and from a String to an enum field. If you need to go beyond the default conversion you can override the toString method in your enum class to control the String shown in your view.
There are no comments for this entry.
[Add Comment]