NOTE: This is outdated information that applies only to Tapestry 4.

Imagine you wanted to create a Refill page where a user can refill his prepaid account. On some page the user shall be able to select the amount to book to his account.

We'll use Tapestry's PropertySelection component to display a drop-down field to the user where he can select the desired amount. Because we don't want to change our source code every time the guys over at marketing decide that there should be new values to choose from, we'll source out the possible values of our drop-down field to a .properties file.

This results in the following files:

Refill.java page class:

public abstract class Refill extends BasePage {
  
  /* get the message with key "amounts" */
  @Message public abstract String getAmounts();
  
  /* get the message with key "split-char".
   * the explicit split-char is necessary for locales where the comma sign is
   * used for number grouping */
  @Message public abstract String getSplitChar();
  
  public abstract double getAmount();
  
  /* the StringConvertedPropertySelectionModel allows us to supply an array
   * of strings. Each string represents one entry in our drop-down field. If
   * the string contains an equal sign (=), the content before the equal sign
   * will be taken for the label and the content after it will be taken for
   * the value of this element */
  public IPropertySelectionModel getAmountsModel() {
    return new StringConvertedPropertySelectionModel(
      getTopups().split(getSplitChar()));
  }
  
  @Component(bindings = {"value=amount", "model=amountsModel"})
  public abstract PropertySelection getAmountsSelection();
  
  public IPage doSomething() {
    // process the form submission
    return this;
  }
}

Refill.html page template:

<html jwcid="@Shell" title="selection test">
<body jwcid="@Body">
  <form jwcid="@Form" success="listener:doSomething">
    <select jwcid="amountsSelection"></select>
  </form>
</body>
</html>

Refill.properties (to be placed into WEB-INF/):

split-char=,
amounts=\
  $ 1.00=1, \
  $ 2.56=2.56, \
  $ 5.10=5.10

Refill_de.properties (to be placed into WEB-INF/):

split-char=;
# note that in Germany the comma is used for number grouping
amounts=\
  $ 1,00=1; \
  $ 2,56=2.56; \
  $ 5,10=5.10

PropertySelectionFromPropertiesFile (last edited 2011-01-02 16:11:36 by BobHarner)