English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Bundle Tag

JSP Standard Tag Library

The <fmt:bundle> tag makes the specified resource bundle available to the <fmt:message> tag within the <fmt:bundle> tag. This can save 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.w3codebox.Example">
    <fmt:message key="count.one"/>
</fmt:bundle>
<fmt:bundle basename="com.w3codebox.Example" prefix="count.">
    <fmt:message key="title"/>
</fmt:bundle>

Syntax Format

<fmt:bundle baseName="<string>" prefix="<string>"/>

Attribute

The <fmt:bundle> tag has the following attributes:

Attribute Description Is it necessary Default Value
basename Specify the base name of the resource bundle to be loaded Yes None
prefix Specify the prefix of the key attribute of the <fmt:message> tag No None

Program Example

The resource bundle contains locale-specific objects. The resource bundle contains key-value pairs. When your program needs locale-specific resources, you can share all the key-value pairs with all locales, but you can also specify the converted values for the locale. The resource bundle can help provide the content specified for the locale.

A Java resource bundle file contains a series of key-value pairs. The methods we are concerned with involve 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.w;3codebox;
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 files into Example.class, and then place them in a location that the Web application's CLASSPATH can find. 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.w3codebox.Example" prefix="count.">
   <fmt:message key="one">/><br/>
   <fmt:message key="two"/><br/>
   <fmt:message key="three"/><br/>
</fmt:bundle>
</body>
</html>

The running result is as follows:

One 
Two 
Three

Change it to 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 Tag</title>
</head>
<body>
<fmt:bundle basename="com.w3codebox.Example">
   <fmt:message key="count.one"/><br/>
   <fmt:message key="count.two"/><br/>
   <fmt:message key="count.three"/><br/>
</fmt:bundle>
</body>
</html>

The running result is as follows:

One 
Two 
Three

can view<fmt:setLocale>and<fmt:setBundle>for more information.

JSP Standard Tag Library