English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The <fmt:bundle> tag makes the specified resource bundle available to the <fmt:message> tags appearing 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 clave="count.one"/> </bundle:fmt> <fmt:bundle basename="com.w3codebox.Example" prefix="count."> <fmt:message key="title">/> </bundle:fmt>
<fmt:bundle baseName="<string>" prefix="<string>"/>
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 |
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 key-value pairs with all locales, but you can also specify transformed values for the locale. The resource bundle can help provide content for the specified 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 file into Example.class, then place it 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" prefijo="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/bundle" prefijo="fmt" %> <html> <head> <title>JSTL fmt:bundle etiqueta</title> </head> <body> <fmt:bundle basename="com.w3codebox.Example" prefix="count."> <fmt:message key="one">/><br/> <fmt:message clave="two"/><br/> <fmt:message clave="three"/><br/> </bundle:fmt> </body> </html>
如下所示:运行结果
Uno Dos Tres
Cambia por: sin atributo prefix
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefijo="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/bundle" prefijo="fmt" %> <html> <head> <title>JSTL fmt:bundle etiqueta</title> </head> <body> <fmt:bundle basename="com.w3codebox.Example"> <fmt:message clave="count.one"/><br/> <fmt:message clave="count.two"/><br/> <fmt:message clave="count.three"/><br/> </bundle:fmt> </body> </html>
如下所示:运行结果
Uno Dos Tres
puede ver<fmt:setLocale>y<fmt:setBundle>获取更多信息。