<fmt:bundle> Tag
The <fmt:bundle>
tag makes the specified resource bundle available to the <fmt:message>
tags within the <fmt:bundle>
tag. This can save you many steps of specifying the resource bundle for each <fmt:message>
tag.
For example, the following two <fmt:bundle>
blocks will produce the same output:
<fmt:bundle basename="com.tutorialpro.Example">
<fmt:message key="count.one"/>
</fmt:bundle>
<fmt:bundle basename="com.tutorialpro.Example" prefix="count.">
<fmt:message key="title"/>
</fmt:bundle>
Syntax
<fmt:bundle baseName="<string>" prefix="<string>"/>
Attributes
The <fmt:bundle>
tag has the following attributes:
Attribute | Description | Required | Default Value |
---|---|---|---|
basename | Specifies the base name of the resource bundle to be loaded | Yes | None |
prefix | Specifies the prefix for the key attribute of the <fmt:message> tag |
No | None |
Example Program
Resource bundles contain locale-specific objects. Resource bundles contain key-value pairs. When your program needs locale-specific resources, you can share all the key-value pairs for all locales, but you can also specify the translated values for each locale. Resource bundles help provide content specific to a locale.
A Java resource bundle file contains a series of key-value pairs. The method we are interested in involves creating compiled Java classes that inherit from the java.util.ListResourceBundle
class. You must compile these classes and place them in your Web application's CLASSPATH.
Let's define a default resource bundle:
package com.tutorialpro;
import java.util.ListResourceBundle;
public class Example_En extends ListResourceBundle {
public Object[][] getContents() {
return contents;
}
static final Object[][] contents = {
{"count.one", "One"},
{"count.two", "Two"},
{"count.three", "Three"},
};
}
Compile the above file to Example.class
and place it where your Web application's CLASSPATH can find it. Now you can use JSTL to display these three numbers, like this:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<title>JSTL fmt:bundle Tag</title>
</head>
<body>
<fmt:bundle basename="com.tutorialpro.Example" prefix="count.">
<fmt:message key="one"/><br/>
<fmt:message key="two"/><br/>
<fmt:message key="three"/><br/>
</fmt:bundle>
</body>
</html>
The output will be:
One
Two
Three
To remove the prefix attribute:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<title>JSTL fmt:bundle Tag</title>
</head>
<body>
<fmt:bundle basename="com.tutorialpro.Example">
<fmt:message key="count.one"/><br/>
<fmt:message key="count.two"/><br/>
<fmt:message key="count.three"/><br/>
</fmt:bundle>
</body>
</html>
The output will be the same:
One
Two
Three
One
Two
Three
You can refer to <fmt:setLocale> and <fmt:setBundle> for more information.