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
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
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 :
Previous Tutorial : JSTL Format fmt:setTimeZone Tag :: Next Tutorial : JSTL Format fmt:requestEncoding Tag