JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

JSTL Format fmt:message Tag

February 27, 2014 by Krishna Srinivasan Leave a Comment

The <fmt: message> tag maps key to localized message to return the value using the resource bundle specified in the bundle attribute.

Syntax Of <fmt: message> Tag

[code lang=”html”]
<fmt: message attributes> body content </fmt: message>
[/code]

Attributes Of <fmt: message> Tag

Attributes Description
key It is used for specifying the key whose value which we want to display.
bundle Specifies the resource bundle that is used to get the value of the specified key.
var Specifies the variable name to store the retrieved message .
scope The Scope into which the variable has to be set.

Example

Listing 1:Message .java

[code lang=”java”]
package javabeat.net;

import java.util.ListResourceBundle;

public class Message extends ListResourceBundle
{
public Object[ ][ ] getContents()
{
return contents;
}
static final Object[ ][ ] contents =
{
{"color.blue", "blue"},
{"color.pink", "pink"},
{"color.yellow", "yellow"},
};
}
[/code]

Details of the Code
ListResourceBundle is used to manage the list of values. The class Message extends the ListResourceBundle to get list of values by using getContent method.

Listing 2:example.jsp

[code lang=”html”]
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<html>
<head>

<title>Message tag</title>
</head>
<body>

<fmt:setBundle basename="javabeat.net.Message" var="lang"/>

<fmt:message key="color.blue" bundle="${lang}"/><br/>
<fmt:message key="color.pink" bundle="${lang}"/><br/>
<fmt:message key="color.yellow" bundle="${lang}"/><br/>

</body>
</html>
[/code]

Details of the Code

  • <%@ taglib uri=”http://java.sun.com/jsp/jstl/fmt” prefix=”fmt” %> tag is used for formatting the data in jsp page.
  • <fmt: message> tag with key attribute is used to display the specified message.
  • bundle is used to get the value of specified key

Steps for Execution

  • Save this file as example.jsp in your eclipse IDE.
  • Now select this jsp file, right mouse click and select Run as ->Run on server

Output

When the execution process is completed successfully we will get the following output :
fmt message tag output

 

Previous Tutorial : JSTL Format fmt:setTimeZone Tag :: Next Tutorial : JSTL Format fmt:requestEncoding Tag

Filed Under: Java EE Tagged With: JSTL Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved