JSTL Format fmt:message Tag

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

<fmt: message attributes> body content </fmt: message>

Attributes Of <fmt: message> Tag

AttributesDescription
keyIt is used for specifying the key whose value which we want to display.
bundleSpecifies the resource bundle that is used to get the value of the specified key.
varSpecifies the variable name to store the retrieved message .
scopeThe Scope into which the variable has to be set.

Example

Listing 1:Message .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"},
	  };
}

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

<%@ 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>

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

Comments

comments

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.

Speak Your Mind

*