JSTL fmt Tag message Example

JSTL fmt Tag message Example


Posted in : Java Posted on : April 4, 2012 at 8:07 PM Comments : [ 0 ]

In this tutorial you will learn about the JSTL tag.

JSTL fmt Tag message Example

In this tutorial you will learn about the JSTL <fmt:message> tag.

<fmt:message> tag is used to map the key from the key-value paired mapped localized message and returns the value by replacing the key to the respective value.

Attributes of <fmt:message>

  • key : This attribute is an optional attribute that is used for specifying the key (key from the key-value paired localized message) of which value you want to show.
  • bundle : This attribute is an optional attribute that is used for specifying the Localization context in whose resource bundle message key's value you want to show.
  • var : This attribute is an optional attribute that is used for specifying the name of the scoped variable that have stored the localized message.
  • scope : This attribute is an optional attribute and is used for specifying the scope of var.

Example :

An example is being given below will demonstrate you about how to use the <fmt:message> tag in JSP. In this example at first I have created a properties file named Pop_hi_IN.properties that contains the contents in key-value pair. Then I have created a JSP page into which I have loaded the resource bundle using the <fmt:bundle> tag. And then used the <fmt:message> tag with key attribute to map the key and for replacing the key to value from the properties file. So as an output you will see the value of the properties file that are mapped by the key.

Pop_hi_IN.properties

ONE=एक
TWO=दो
THREE=तीन
FOUR=चार
FIVE=पांच

JstlFmtMessage.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:message Tag</title>
</head>
<body>
<p>Hindi (India) Locale</p>
<fmt:setLocale value="hi_IN"/>
<fmt:bundle basename="myProperties.Prop.Pop">
1. <fmt:message key="ONE"/><br/>
2. <fmt:message key="TWO"/><br/>
3. <fmt:message key="THREE"/><br/>
4. <fmt:message key="FOUR"/><br/>
5. <fmt:message key="FIVE"/><br/>
</fmt:bundle>
</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 JstlFmtMessage.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 JstlFmtMessage.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 the execution process will be completed successfully an output will be displayed on your eclipse browser as :

NOTE : If the appropriate font will not available in your system then the output may differ than the original.

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics