JSTL Format Tag fmt:bundle Example
- Details
- Written by Nam Ha Minh
- Last Updated on 31 August 2019   |   Print Email
In this post, you will learn how to use the <fmt:bundle> tag in the JSTL format tags library with code example.
You know, the <fmt:bundle> tag loads the resource bundle to be used in the tag body. The resource bundle this tag loads contains the key-value pairs which are used to internationalize or localize the web application. Subsequently, the tag <fmt:message> is used to display the internationalized or localized messages specified in the resource bundle to the output.
JSTL <fmt:bundle> Syntax:
<fmt:bundle baseName="<string>" prefix="<string>"/>
Attributes:
Name | Required | Type | Description |
baseName | True | java.lang.String | Resource bundle’s fully qualified name. Follows the Java’s fully qualified class name convention (‘.’ is used to separate the package names). |
prefix | False | java.lang.String | When used with <fmt:message> this attribute specifies the value to be prepended in the key value so that each time we do not have to provide the prefix repeatedly. |
JSTL <fmt:bundle> Example:
The below example loads the resource bundle located in the package net.codejava.jstl and displays couple of the messages from the resource bundle messages.properties.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/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><fmt:bundle> Demo</title> </head> <body> <h1><fmt:bundle> Demo</h1> <fmt:bundle basename="net.codejava.jstl.messages"> <fmt:message key="codejava.welcome.text"/><br/> <fmt:message key="codejava.description.text"/> </fmt:bundle> <br/><br/> <p>With prefix</p> <br/> <fmt:bundle basename="net.codejava.jstl.messages" prefix="codejava."> <fmt:message key="welcome.text"/><br/> <fmt:message key="description.text"/> </fmt:bundle> </body> </html>
Note that the first time we have not used the prefix attribute hence we needed to specify the full key name to display our messages. To avoid this in the next example we specified the prefix.
Output:
The above example loads a resource bundle messages.properties using the tag <fmt:bundle> located in the package net.codejava.jstl and displays two messages using the <fmt:message> tag.
Recommended Usage of JSTL <fmt:bundle> tag:
To display the localized messages from a resource bundle. Use the prefix attribute to specify a prefix value of the key to be loaded and display instead of repeating the prefix every time.
Other JSTL Format Tags:
formatDate | formatNumber | message | param | parseDate | parseNumber | requestEncoding | setBundle | setLocale | setTimeZone | timeZone
Comments