<fmt:bundle> Tag
The <fmt:bundle> tag makes the specified resource bundle available to <fmt:message> tags that appear 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.example.Example">
<fmt:message key="count.one"/>
</fmt:bundle>
<fmt:bundle basename="com.example.Example" prefix="count.">
<fmt:message key="title"/>
</fmt:bundle>
Syntax Format
<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 |
Program Example
Resource bundles contain locale-specific objects. Resource bundles contain key-value pairs. When your program needs locale-specific resources, you can share all the keys across all locales, but you can also specify translated values for the locale. Resource bundles can help provide content specific to a locale.
A Java resource bundle file contains a series of key-value pairs. The method we are concerned with involves creating compiled Java classes that extend the java.util.ListResourceBundle class. You must compile these classes and place them in the CLASSPATH of your Web application.
Let us define a default resource bundle:
package com.example;
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 into Example.class, then place it where the CLASSPATH of your Web application 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 标签</title>
</head>
<body>
<fmt:bundle basename="com.example.Example" prefix="count.">
<fmt:message key="one"/><br/>
<fmt:message key="two"/><br/>
<fmt:message key="three"/><br/>
</fmt:bundle>
</body>
</html>
The output is as follows:
One Two Three
Change it to have no 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 标签</title>
</head>
<body>
<fmt:bundle basename="com.example.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 is as follows:
One Two Three
You can view<fmt:setLocale>and<fmt:setBundle>to get more information.
Other Extensions
JSP Standard Tag Library