JSF 2 f:convertNumber Tag

JSF 2 f:convertNumber Tag


Posted in : Java Posted on : June 4, 2012 at 4:36 PM Comments : [ 0 ]

In this tutorial you will learn about the convertNumber tag of JSF core tag library.

JSF 2 f:convertNumber Tag

In this tutorial you will learn about the convertNumber tag of JSF core tag library.

convertNumber tag is used to convert the string into the specified number format. Instance of NumberConverter is instantiated on the closest component on the use of this tag. This tag may also acted as an validator on the component inside in which it is used. This tag has various attributes that are used to format the number in different formats.

Attributes of convertNumber tag

  • currencyCode : This is not a required attribute but, it is used/applied when the currency formatting is required. It formats the currency into the specified ISO 4217 currency code. When this attribute is used the type attribute should be the "currency" i.e type="currency".
  • currencySymbol : This is not a required attribute but, it is used/applied when the currency formatting is being done. It sets the currency symbol on the formatted currency in this attribute the type attribute should also be specified as "currency" i.e type="currency".
  • groupingUsed : This is not a required attribute but, it is used when the formatted output is required to grouping separators. True appearance of this attribute specifies the formatted output will formatted with the grouping separators. Default value of this attribute is "true".
  • integerOnly : This attribute is not a required attribute but, it is used when only the value's integer part is required to format and parsed (processed). Default value of this attribute is "false".
  • locale : This is not a required attribute, but it is used when the value's are required to format in a specified predefined Locale. The expressions must evaluate to a java.util.Locale or the valid String must be the same as specified for the first and second argument of java.util.Locale(String language, String country).
  • maxFractionDigits : This is not a required attribute but, is used to format the fractional part of the output of a value. It formats the value's output's fractional part by a specified maximum number of digits.
  • maxIntegerDigits : This is not a required attribute but, is used to format the integer part of the output of a value. It formats the value's output's integer part by a specified maximum number of digits.
  • minFractionDigits : This is not a required attribute but, is used to format the fractional part of the output of a value. It formats the value's output's fractional part by a specified minimum number of digits.
  • minIntegerDigits : This is not a required attribute but, is used to format the integer part of the output of a value. It formats the value's output's integer part by a specified minimum number of digits.
  • pattern : This is not a required attribute but, it may be used when a custom formatting pattern is required. The formatting pattern specifies how the number string will be formatted and parsed as output.
  • type : This is not a required attribute but, it is used to specify into which the number string will be formatted and parsed. The valid formatting type is "number", "currency", and "percent". Default value of this attribute is "number".
  • binding : This is not a required attribute. It is a ValueExpression to evaluate the instance of javax.faces.convert.NumberConverter.
  • for :  This is not a required attribute. This attribute refers to the value as object specified in the components. It may use any of the specified object value of the tag inside which this tag is nested.

Example :

Here an example is being given below will demonstrate you about how to use the convertNumber tag of JSF core tag library. This example explains how number strings can be formatted into the different formats. In this example I have used the various of attributes of this tag that helps in formatting the number string into different formats. I have created the two JSP pages into which one JSP pages is created for taking the input by the user and the other one is created for displaying the output of the number string value into the different number formats. And also created a Java bean class to set and get the value input by the user.

Directory structure of this project

NumberBean.java

package devmanuals;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name="numberBean")
@RequestScoped

public class NumberBean {
double number;

public double getNumber() {
return number;
}

public void setNumber(double number) {
this.number = number;
}

}

inputNumber.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Input Number</title>
</head>
<body>
<f:view>
<h:form>
Enter Number : 
<h:inputText id="num" value="#{numberBean.number}" >
</h:inputText><br>
<br>
<h:message for="num" style="color:red" />
<br><br>
<h:commandButton value="Submit" action="outputNumber" />
</h:form>
</f:view>
</body>
</html>

outputNumber.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Number output</title>
</head>
<body>
<f:view>
Number with maximum integer digits : 
<h:outputText value="#{numberBean.number}" >
<f:convertNumber maxIntegerDigits="2"/>
</h:outputText><br>
Number with the specified pattern : 
<h:outputText value="#{numberBean.number}" >
<f:convertNumber pattern="#0.0" />
</h:outputText>
</f:view>
</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
<jsp:forward page="/jspPages/inputNumber.jsf"></jsp:forward>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>jsfConvertNumber</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>/faces/*</url-pattern>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
</web-app>

There is no need to write the faces-config.xml file in this example because we are using annotation to managed bean.

Output :

When you will execute the above example //http://localhost:8080/jsfConvertNumber/ you will get the output as follows :

1. First of all the input page will be displayed to enter the number as follows :

2. When you will input the number and clicked on submit button you will get the output as follows :

3. But when you will input the value like below the output will be as follows :

Download Source Code (Here I am giving a WAR file. To run this example simply download it from the provided link and import it into your Eclipse and deploy on the server.)

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics