This page describes how to format Date, Time, Floats, Strings, currencies etc using standard components and extensions.

Page class

The page class provides the following properties:

        public Date getDateProperty() {
                return new Date();
        }

        public double getCurrencyValue() {
                return 2.2;
        }

        public String getCurrency() {
                return "EUR";
        }

Output of Date, Time or DateTime

The output component of Tapestry uses java.text.Format to format a value. === Using a literal ==

<t:output format="literal:MMM yyyy" value="dateProperty"/>

Output result: Feb 2009

Using a resource bundle entry

The following approach allows to format a date adapted to the users local.

<t:output format="message:month_year" value="dateProperty"/>

The format String is picked from the resource bundle. Extract of the resource bundle (for example app.properties)

month_year=MMM YYYY

Output result: Feb 2009

Using a String.format expression

In order to achieve this, we need to add a new binding expressions. This is explained on the following page. Tapestry5HowToAddMessageFormatBindingPrefix - Creating a binding expression using String.format]

${format:common_month_year=dateProperty} common_month_year is an entry of the resource bundle Extract of the resource bundle (for example app.properties)

common_month_year=%1$tb %1$tY

Output result: Feb 2009

Formatting of numbers

Tapestry's output component

The default output component has limitations for number formatting. In order to format a decimal you need to pass an instance of DecimalFormat to the component. First provide the method in the class

 public DecimalFormat getCurrencyFormat(){
  return new DecimalFormat("0.00");
 }

Usage

<t:output format="numberFormat" value="currencyValue"/>

There is an extension to this component, allowing a simple definition of formats.

Tapestry5OutputLocaleNumber - Localized output component

Using a String.format expression

In order to achieve this, we need to add a new binding expressions. This is explained on the following page. Tapestry5HowToAddMessageFormatBindingPrefix - Creating a binding expression using String.format

${format:twodigit=currencyValue,currency}

twodigit is an entry of the resource bundle Extract of the resource bundle (for example app.properties)

twodigit=%.2f

Output result: 2.20

Other formatting

The binding extension Tapestry5HowToAddMessageFormatBindingPrefix uses String.format. It supports all kinds of formattings and can even pass multiple values to the formatting String. Sample to format a currency: 2.20 EUR Resource bundle entry

currency=%.2f %s

Usage

${format:currency=currencyValue,currency}

Tapestry5HowToFormatDateTimeEtc (last edited 2009-09-20 23:20:07 by localhost)