<fmt:bundle>
《fmt:bundle》标记将使指定的包可用于发布nding 《fmt:bundle》和《/fmt:bundle》标记之间的所有《fmt:message》 《/fmt:bundle》标记。 这样,您无需为每个《fmt:message》标记指定资源包。
例如,以下两个
<fmt:bundle basename = "com.iowiki.Example">
<fmt:message key = "count.one"/>
</fmt:bundle>
<fmt:bundle basename = "com.iowiki.Example" prefix = "count.">
<fmt:message key = "title"/>
</fmt:bundle>
属性 (Attribute)
《fmt:bundle》标签具有以下属性 -
属性 | 描述 | 需要 | 默认 |
---|---|---|---|
basename | 指定要加载的资源包的基本名称。 | Yes | None |
Prefix | 要添加到 | No | None |
例子 (Example)
资源包包含特定于语言环境的对象。 资源包包含key/value对。 当您的程序需要特定于语言环境的资源时,您将保留所有语言环境共有的所有键,但您可以具有特定于语言环境的已翻译值。 资源包有助于为区域设置提供特定内容。
Java资源包文件包含一系列key-to-string mappings 。 我们关注的方法涉及创建扩展java.util.ListResourceBundle类的已编译Java类。 您必须编译这些类文件,并使它们可用于Web应用程序的类路径。
让我们定义一个默认资源包,如下所示 -
package com.iowiki;
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"},
};
}
让我们编译上面的类Example.class并使其在Web应用程序的CLASSPATH中可用。 现在,您可以使用以下JSTL标记显示三个数字,如下所示 -
<%@ 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.iowiki.Example" prefix = "count.">
<fmt:message key = "one"/><br/>
<fmt:message key = "two"/><br/>
<fmt:message key = "three"/><br/>
</fmt:bundle>
</body>
</html>
上面的代码将生成以下结果 -
One
Two
Three
尝试上面没有前缀的例子如下 -
<%@ 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.iowiki.Example">
<fmt:message key = "count.one"/><br/>
<fmt:message key = "count.two"/><br/>
<fmt:message key = "count.three"/><br/>
</fmt:bundle>
</body>
</html>
上面的代码将生成以下结果 -
One
Two
Three
检查《fmt:setLocale》和《setBundle》标签以了解完整的概念。