Book Image

Tapestry 5: Building Web Applications

Book Image

Tapestry 5: Building Web Applications

Overview of this book

Table of Contents (17 chapters)
Tapestry 5
Credits
About the Author
About the Reviewers
Preface
Foreword
Where to Go Next

Using Enumerations for Radio Component Values


Right now, when the Registration page is rendered, none of the radio buttons are selected. If you want, you can provide a default option by giving the gender property an initial value, like this:

private String gender = "F";

Now the radio button labeled "Female" will be pre-selected.

The page works properly, but having a gender specified as an arbitrary string isn't a good design. For a set of mutually exclusive options like this, it would be better to have an enumeration. So let's add the following simple enumeration to the com.packtpub.celebrities.model package:

package com.packtpub.celebrities.model;
public enum Gender
{
MALE, FEMALE
}

We can now modify all the gender-related code to work with the new Gender type instead of String:

private Gender gender;
...
public Gender getGender()
{
return gender;
}
public void setGender(Gender gender)
{
this.gender = gender;
}

In case you want to provide a default value, you can do it like this:

private...