JSTL fmt Tag parseNumber Example

JSTL fmt Tag parseNumber Example


Posted in : Java Posted on : April 10, 2012 at 5:48 PM Comments : [ 0 ]

In this tutorial you will learn about the JSTL fmt parseNumber tag in JSP.

JSTL fmt Tag parseNumber Example

In this tutorial you will learn about the JSTL fmt parseNumber tag in JSP.

In JSP a string format of number, currency, or percentage is parsed using the JSTL fmt tag library <fmt:parseNumber> tag.

Attributes of <fmt:parseNumber>

  • value : This is an optional attribute. This attribute specifies the value of string to be parsed.
  • type : This is an optional attribute specifies the type of string is required to be parsed i.e. number, currency, or percent.
  • pattern : This is an optional attribute that specifies the custom formatting pattern of the string to be parsed
  • parseLocale : This is an optional attribute that specifies Locale of which default formatting pattern for number, currency, percentages respectively is to be used on the parse operation or, the pattern that is defined through the pattern attribute is used.
  • integerOnly: This is an optional attribute and used when you are required only the integer portion to be parsed of the given value.
  • var : This is an optional attribute that defines the name of a scoped variable, kept the parsed result in the java.lang.Number type.
  • scope : This is an optional attribute that may be used for specifying the scope of the attribute 'var'.

Example :

Here I am giving a simple example which will demonstrate you about the use of JSTL fmt <fmt:parseNumber> tag. In this example I have created a HTML page where the string that has to be parsed will be given then created a JSP page where I have written the code for parsing these strings, for this I have used the <fmt:parseNumber> with different attributes.

parseNumber.html

<!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>parseNumber</title>
</head>
<body>
<form action="JstlFmtParseNumber.jsp">
<table>
<tr>
<td>Enter Number </td>
<td><input type="text" name="num"/></td>
</tr>
<tr>
<td>Enter percent </td>
<td><input type="text" name="per"/></td>
</tr>
<tr>
<td>Enter currency </td>
<td><input type="text" name="cur"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="submit"/></td>
</tr>
</table>
</form>
</body>
</html>

JstlFmtParseNumber.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!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>JSTL fmt:parseNumber Tag</title>
</head>
<body>
<h3>String Representation</h3>
<table>
<tr>
<td><b>Number :</b></td> 
<td><fmt:parseNumber value="${param.num}" type="number" integerOnly="true"/></td>
</tr>
<tr>
<td><b>Currency :</b></td>
<td><fmt:parseNumber value="${param.cur}" type="currency" /></td>
</tr>
<tr>
<td><b>Percent :</b></td>
<td><fmt:parseNumber value="${param.per}" type="percent" /></td>
</table>
</body>
</html>

How to run this example

Here I am using an IDE Eclipse so I am giving the process of executing this example in perspective of Eclipse. Before executing this example you will have needed to add the following jar files :

  • jstl.jar
  • standard.jar

After adding of these jar files you may execute your program in the following ways :

  • Select JstlFmtParseNumber.jsp file of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • On the Eclipse Editor go to your JstlFmtParseNumber.jsp -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • Go to Run button look at the toolbar in green color and click -> Choose your server -> Finish.
  • A simplest way to execute the example in Eclipse is to use the CTRL+F11 key -> Run On Server -> Choose your server -> Finish

NOTE : In all of the above execution processes you may start the server first and stop the server each time after the execution if not, each time you will may prompted to a dialog box to Restart the server in Eclipse.

Output :

When you will execute the HTML page following page will be displayed for you :

When you will enter the appropriate value and will click on submit button then output will be as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics