<fmt:bundle>
標籤將使指定的包可用於在<fmt:bundle>
和</fmt:bundle>
標籤之間發生的所有<fmt:message>
標籤。 因此,不需要為每個<fmt:message>
標籤指定資源綁定。
例如,以下兩個<fmt:bundle>
塊將產生相同的輸出 -
<fmt:bundle basename = "com.zaixian.example">
<fmt:message key = "count.one"/>
</fmt:bundle>
<fmt:bundle basename = "com.zaixian.example" prefix = "count.">
<fmt:message key = "title"/>
</fmt:bundle>
屬性
<fmt:bundle>
標籤具有以下屬性 -
屬性 | 描述 | 必需 | 默認 |
---|---|---|---|
basename |
指定要加載的資源綁定的基本名稱。 | 是 | — |
prefix |
在<fmt:message> 子標籤中為每個鍵名稱添加的值 |
否 | — |
示例
資源綁定包含區域特定的對象。 資源綁定包含鍵/值對。 當程式需要特定於區域設置的資源時,可以將所有鍵保留在所有區域設置中,但可以使用指定區域設置的翻譯值。 資源綁定有助於向區域設置提供特定的內容。
Java資源包檔包含一系列鍵到字串的映射。 我們關注的方法調用涉及創建擴展java.util.ListResourceBundle
類的Java類。 必須編譯這些類檔並使其可用於Web應用程式的類路徑。
下麵來看如何定義一個默認資源綁定如下 -
檔:Example_Cn.java -
package com.zaixian;
import java.util.ListResourceBundle;
public class Example_Cn extends ListResourceBundle {
public Object[][] getContents() {
return contents;
}
static final Object[][] contents = { { "count.one", "一個" }, { "count.two", "兩個" }, { "count.three", "三個" }, };
}
編譯上面的Example_Cn
類,並使其在Web應用程式的CLASSPATH
中可用。現在可以使用以下JSTL標籤來顯示三個數字,如下所示:
檔:fmt_bundle.jsp -
<%@ 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.zaixian.Example_Cn" prefix = "count.">
<fmt:message key = "one"/><br/>
<fmt:message key = "two"/><br/>
<fmt:message key = "three"/><br/>
</fmt:bundle>
</body>
</html>
這將產生以下結果 -
一個
兩個
三個
嘗試上面不使用首碼的示例,如下 -
檔:fmt_bundle2.jsp -
<%@ 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.zaixian.Example_Cn" prefix = "count.">
<fmt:message key = "one"/><br/>
<fmt:message key = "two"/><br/>
<fmt:message key = "three"/><br/>
</fmt:bundle>
</body>
</html>
這將產生以下結果 -
一個
兩個
三個
可通過查看<fmt:setLocale和<setBundle標籤以瞭解完整的概念。
上一篇:
Jstl教學
下一篇:
JSP+MySQL實例