<fmt:bundle> 標簽
<fmt:bundle> 標簽
<fmt:bundle>標簽將指定的資源束對出現在<fmt:bundle>標簽中的<fmt:message>標簽可用。這可以使您省去為每個<fmt:message>標簽指定資源束的很多步驟。
舉例來說,下面的兩個<fmt:bundle>塊將產生同樣的輸出:
<fmt:bundle basename="com.runoob.Example"> <fmt:message key="count.one"/> </fmt:bundle> <fmt:bundle basename="com.runoob.Example" prefix="count."> <fmt:message key="title"/> </fmt:bundle>
語法格式
<fmt:bundle baseName="<string>" prefix="<string>"/>
屬性
<fmt:bundle>標簽有如下屬性:
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
basename | 指定被載入的資源束的基礎名稱 | 是 | 無 |
prefix | 指定<fmt:message>標簽key屬性的前綴 | 否 | 無 |
程序示例
資源束包含區域特定對象。資源束包含鍵值對。當您的程序需要區域特定資源時,可以將所有的關鍵詞對所有的locale共享,但是也可以為locale指定轉換后的值。資源束可以幫助提供指定給locale的內容。
一個Java資源束文件包含一系列的鍵值對。我們所關注的方法涉及到創建繼承自java.util.ListResourceBundle 類的已編譯Java類。您必須編譯這些類然后放在您的Web應用程序的CLASSPATH中。
讓我們來定義一個默認的資源束:
package com.runoob; 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能找到的地方?,F在可以使用JSTL來顯示這三個數字了,就像這樣:
<%@ 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.runoob.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
將其改為無prefix屬性:
<%@ 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.runoob.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>和<fmt:setBundle>來獲取更多信息。