JSTL Format Tag fmt:setBundle Example
- Details
- Written by Nam Ha Minh
- Last Updated on 31 August 2019   |   Print Email
This post helps you understand and use the <fmt:setBundle> tag in the JSTL format tags library with code example.
The <fmt:setBundle> tag loads the resource bundle to be used in the entire JSP page as opposed to <fmt:bundle> whose resource bundle can only be used within its 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:setBundle> Syntax:
<fmt:setBundle baseName="<string>" var="<string>" scope="<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). |
var | False | java.lang.String | Name of the scoped variable which stores the resource bundle. |
scope | False | java.lang.String | Scope of the resource bundle to be stored. |
JSTL <fmt:setBundle> Example:
The main difference between <fmt:setBundle> and <fmt:bundle> is that, the resource bundle loaded from <fmt:setBundle>can be used anywhere in the whole JSP page. The following example demonstrates this difference precisely:
<%@ 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:setBundle> Demo</title> </head> <body> <h1><fmt:setBundle> Demo</h1> <fmt:setBundle basename="net.codejava.jstl.messages" var="bundle"/> <fmt:message key="codejava.setbundle.welcome.text" bundle="${bundle}"/><br/> <fmt:message key="codejava.setbundle.description.text" bundle="${bundle}"/><br/> </body> </html>
Note that the <fmt:message> tags used to display localized messages are outside of the tag <fmt:setBundle>. We specified which bundle to be used to load messages using the attribute bundle in the <fmt:message> tags.
Output:
Recommended Usage of JSTL <fmt:setBundle> tag:
If our JSP page have localized messages to be loaded from only one resource bundle, it is obviously better choice to use <fmt:setBundle> to load the bundle one time. Subsequently, messages are displayed using <fmt:message> tags using the resource bundle loaded.
However, if we need to display localized messages from different bundles, we need to use <fmt:bundle> to load the resource bundles we required and displaying messages using <fmt:message> tag within the tag body of the <fmt:bundle>.
Other JSTL Format Tags:
bundle | formatDate | formatNumber | message | param | parseDate | parseNumber | requestEncoding | setLocale | setTimeZone | timeZone
Comments