Book Image

JSF 2.0 Cookbook

By : Anghel Leonard
Book Image

JSF 2.0 Cookbook

By: Anghel Leonard

Overview of this book

<p>JavaServer Faces is a Java-based Web application framework intended to simplify development of user interfaces for Java EE applications. You may already be aware of the laborious search through reference guides and documentation to develop your JSF applications. With the JSF Cookbook, you can find solutions to the most common JSF problems in a quick and easy way.<br /><br />This book will cover all the important aspects involved in developing JSF applications. It provides clear instructions for getting the most out of JSF and offers many exercises to build impressive desktop-style interfaces for your web applications. Develop JSF applications starting with simple recipes and gradually moving on to complex recipes.<br /><br />We discuss all of the fundamental aspects of JSF applications. Once you locate your desired topic, you can try to find a recipe that corresponds to your problem. <br /><br />We start off with the simple concepts of Converters, validators and file management. We then work our way with various resources such as CSS, JavaScript, and images to improve your web applications. You will learn to build simple and complex custom components to suit your needs. Next, you get to exploit AJAX as well as implement internationalization and localization for your JSF applications. We then look into ensuring security for your applications and perform testing of your applications. You also get to learn all about Facelets and explore the newest JSF 2.0 features. Finally you get learn a few integrations such as JSTL with JSF, Spring with JSF, and Hibernate with JSF. All these concepts are presented in the form of easy-to-follow recipes.<br /><br />Each chapter discusses separate types of recipes and they are presented with an increasing level of complexity from simple to advanced. All of these recipes can be used with JSF 1.2 as well.</p>
Table of Contents (19 chapters)
JSF 2.0 Cookbook
Credits
About the Author
About the Reviewer
Preface
Index

Standard converters for numbers


Numbers are a generic notion used to quantify many things, such as age, salary, percent, currency, custom pattern, and so on. Also, we know that numbers can be integers, floats, doubles, and so on. Depending on what we represent, we know what kind of number to use and how to write it in the correct format and with the correct symbols attached. In this recipe you will see how to accomplish this task using JSF standard capabilities. For this we will take a generic double number and we will output it to represent different things.

Getting ready

We developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library.

How to do it...

Converting numbers and applying basic formats to them are tasks that can be accomplished by the f:convertNumber JSF converter. This converter can be customized using a set of attributes, listed next:

Attribute name

Description

type

Represents the type of number. By default this type is set to number, but you can set it to currency or percent.

pattern

Represents the decimal format pattern used to convert this number.

locale

Represents the locale to be used for displaying this number. The user's current locale is overridden.

maxIntegerDigits

Represents the maximum number of integer digits to display

minIntegerDigits

Represents the minimum number of integer digits to display.

maxFractionDigits

Represents the maximum number of fractional digits to display.

minFractionDigits

Represents the minimum number of fractional digits to display.

currencyCode

Represents a three-digit international currency code when the attribute type is currency.

currenySymbol

Represents a symbol, like $, to be used when the attribute type is currency.

integerOnly

Set the value of this attribute to true, if you want to ignore the fractional part of a number. By default it is set to false.

groupingUsed

Set the value of this attribute to true, if you want to use a grouping symbol like comma or space. By default it is set to true.

Now, let's suppose that we have the number 12345.12345 (five integer digits and five fraction digits). The following code will output this number using the f:convertNumber converter and the previously listed attributes:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
<h:head>
  <title>Standard converters for numbers - format numbers</title>
</h:head>
<h:body>
  <b><h:outputText value="-Formatting the double value 12345.12345-"/></b><br />
        <!-- Format as 00000.00000 -->
        <h:outputText value="Format as 00000.00000: "/>
        <h:outputText value="#{numbersBean.doubleNumber}">
            <f:convertNumber type="number" maxIntegerDigits="5" 
                             maxFractionDigits="5"
                             groupingUsed="false"/>
        </h:outputText>
        <br />
        <!-- Format as 00000 -->
        <h:outputText value="Format as 00000: "/>
        <h:outputText value="#{numbersBean.doubleNumber}">
            <f:convertNumber type="number" maxIntegerDigits="5" 
                             maxFractionDigits="0"/>
        </h:outputText>
        <br />
        <!-- Format as currency -->
        <h:outputText value="Format as currency: "/>
        <h:outputText value="#{numbersBean.doubleNumber}">
            <f:convertNumber type="currency" currencySymbol="$" 
                             maxIntegerDigits="5" 
                             maxFractionDigits="2"/>
        </h:outputText>
        <br />
        <!-- Format as percent -->
        <h:outputText value="Format as percent: "/>
        <h:outputText value="#{numbersBean.doubleNumber}">
            <f:convertNumber type="percent" maxIntegerDigits="5" 
                             maxFractionDigits="5"/>
        </h:outputText>
        <br />
        <!-- Format as pattern #####,00% -->
        <h:outputText value="Format as pattern #####,00%: "/>
        <h:outputText value="#{numbersBean.doubleNumber}">
             <f:convertNumber pattern="#####,00%"/>
        </h:outputText>
    </h:body>
</html>

The NumbersBean is the managed bean, as shown next:

package numbers;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class NumbersBean {
    private double doubleNumber = 12345.12345;
    public double getDoubleNumber(){
        return this.doubleNumber;
    }
    public void setDoubleNumber(double doubleNumber){
        this.doubleNumber=doubleNumber;
    }
}

The output will be as follows:

-Formatting the double value 12345.12345
Format as 00000.00000: 12345.12345 
Format as 00000: 12,345 
Format as currency: $12,345.12 
Format as percent: 34,512.345% 
Format as pattern #####,00%: 1,23,45,12%

How it works...

The number is displayed corresponding to the formatting attributes. The parts of the number that don't correspond to the conversion's restrictions are ignored or an error message is generated.

There's more...

Notice that we have used the f:convertNumber with the h:outputText component, but you can follow the same logic to use with the h:inputText component. These two components are the most used in conjunction with the f:convertNumber converter.

See also

The code bundled with this book contains a complete example of this recipe. The project can be opened with NetBeans 6.8 and is named: Standard_converters_for_numbers.