☰ See All Chapters |
JSF <f:convertNumber> Tag
<f:convertNumber>is used to convert user input data into a different Number formats. Following are the different attributes of this tag which help in displaying in different format and patterns.
Attribute | Possible Values |
type | Currency, percentage, number |
minFractionDigits | Minimum number of digits in the fractional part |
maxFractionDigits | Maximum number of digits in the integer part |
locale | Any locale value |
Patter | Custom formatting pattern. If value in the property of managed bean is 12345, then form below patters would produce the conversion as below: +91##### ➠ +9112345 ###AA## ➠ 123AA45 ###,# ➠ 123,45 |
JSF <f:convertNumber> Tag Example
pom.xml
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.java4coding</groupId> <artifactId>JSF_convertNumber</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.2.6</version> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-impl</artifactId> <version>2.2.6</version> </dependency> </dependencies> <build> <finalName>JSF_convertNumber</finalName> </build> </project> |
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "https://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> </web-app> |
HelloBean.java
package com.java4coding;
import java.util.Date; import javax.faces.bean.ManagedBean;
@ManagedBean public class HelloBean {
private int userId; private String name; private double percent; private int salary; private long zipcode;
public int getUserId() { return userId; }
public void setUserId(int userId) { this.userId = userId; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getPercent() { return percent; }
public void setPercent(double percent) { this.percent = percent; }
public int getSalary() { return salary; }
public void setSalary(int salary) { this.salary = salary; }
public long getZipcode() { return zipcode; }
public void setZipcode(long zipcode) { this.zipcode = zipcode; }
public String display() { return "second"; }
} |
first.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:h="https://xmlns.jcp.org/jsf/html" xmlns:f="https://xmlns.jcp.org/jsf/core">
<h:head></h:head> <h:body style="background-color:yellow"> <h:form> <h1>Please enter your details</h1> <h:panelGrid columns="3" align="left"> <h:outputText value="Enter your ID" /> <h:inputText id="uid" value="#{helloBean.userId}" converterMessage="Please enter integer value" /> <h:message for="uid" style="color:red" />
<h:outputText value="Enter your name" /> <h:inputText id="nme" value="#{helloBean.name}" /> <h:message for="nme" style="color:red" />
<h:outputText value="Enter your percentage" /> <h:inputText id="per" value="#{helloBean.percent}" converterMessage="Please enter double value eg. 11.9" /> <h:message for="per" style="color:red" />
<h:outputText value="Enter your salary" /> <h:inputText id="sal" value="#{helloBean.salary}" converterMessage="Please enter integer value eg. 1234" /> <h:message for="sal" style="color:red" />
<h:outputText value="Enter zipcode" /> <h:inputText id="zc" value="#{helloBean.zipcode}" converterMessage="Please enter long value eg. 123456" /> <h:message for="zc" style="color:red" />
<h:commandButton value="SUBMIT" action="#{helloBean.display}" /> <h:commandButton type="reset" value="Reset" /> </h:panelGrid>
</h:form> </h:body> </html> |
second.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:h="https://xmlns.jcp.org/jsf/html" xmlns:f="https://xmlns.jcp.org/jsf/core">
<h:head></h:head> <h:body style="background-color:wheat"> <h2> Displaying details in custom format <br />
<h:panelGrid columns="2">
User ID is <h:outputText value="#{helloBean.userId}"> <f:convertNumber pattern="###,#" /> </h:outputText>
Name is <h:outputText value="#{helloBean.name}" />
Percentage in default format is <h:outputText value="#{helloBean.percent}" />
Percentage in custom format is <h:outputText value="#{helloBean.percent}"> <f:convertNumber minFractionDigits="1" maxFractionDigits="2" /> </h:outputText>
Salary in default format is <h:outputText value="#{helloBean.salary}" />
Salary in default currency <h:outputText value="#{helloBean.salary}"> <f:convertNumber type="currency" /> </h:outputText>
Salary in US currency <h:outputText value="#{helloBean.salary}"> <f:convertNumber type="currency" locale="en_US" /> </h:outputText>
Salary in Indian currency <h:outputText value="#{helloBean.salary}"> <f:convertNumber type="currency" locale="en_IN" /> </h:outputText>
Salary in Britain currency <h:outputText value="#{helloBean.salary}"> <f:convertNumber type="currency" locale="en_GB" /> </h:outputText>
Zipcode is <h:outputText value="#{helloBean.zipcode}" />
</h:panelGrid> </h2> </h:body> </html> |
Eclipse Project Directory Structure
Output
All Chapters