f:convertNumber Tag in JSF

July 4, 2008

JSF

What is the use?

This tag is used to register the NumberConverter instance on the enclosing component. This class is responsible to convert String to java.util.Number object and vice-versa.

JSP file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@page pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<html>
    <body>
        <f:view>
            <h:form>
                <h:message for="number" />
                <h:inputText id="number" value="#{JavaBeatJsfBean.number}">
                    <f:convertNumber currencySymbol="$" type="currency" groupingUsed="#{false}"/>
                </h:inputText>
                <h:commandButton value="Submit" type="submit" action="#{JavaBeatJsfBean.submit}"/>
            </h:form>
        </f:view>
    </body>
</html>

JSF Bean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package javabeat.jsf;
 
/**
 * source : www.javabeat.net
 */
public class JavaBeatJsfBean {
    private String number;
 
    public String getNumber() {
        return number;
    }
 
    public void setNumber(String number) {
        this.number = number;
    }
    public String submit()
    {
        System.out.println(this.number);
        return "";
    }
}

faces-config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version='1.0' encoding='UTF-8'?>
 
<faces-config version="1.2"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
 
    <managed-bean>
    <managed-bean-name>JavaBeatJsfBean</managed-bean-name>
    <managed-bean-class>javabeat.jsf.JavaBeatJsfBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>
 
 
</faces-config>

Description

currencyCode : In this attribute ISO 4217
currency code is set that will be applied when converting currency values.
currencySymbol : This sets the currency symbol that will be applied when
formatting currency values.

groupingUsed : This is the boolean attribute and is used to specify
whether the output will contain grouping seperators. Its default value is
“true”.

integerOnly : This is the boolean attribute and is used to specify whether
only the integer part will be parsed.Its default value is “false”.

locale : This is used to specify the name of the locale for which we have to
format the number.

maxFractionDigits : It is used to specify the maximum no. of digits in the
fractional part that will be formated.

maxIntegerDigits : It is used to specify the maximum no. of digits in the
integer part that will be formated.

minFractionDigits : It is used to specify the minimum no. of digits in the
fractional part that will be formated.

minIntegerDigits : It is used to specify the minimum no. of digits in the
integer part that will be formated.

pattern : This is used to set the formatting pattern.

type : It is used to specify the type of formatting. Its valid values are “number”
“currency” “percentage”.
Its default value is “number”.

email

Comments

comments