<fmt:bundle> 標(biāo)簽
JSP 標(biāo)準(zhǔn)標(biāo)簽庫(kù)
<fmt:bundle>標(biāo)簽將指定的資源束對(duì)出現(xiàn)在<fmt:bundle>標(biāo)簽中的<fmt:message>標(biāo)簽可用。這可以使您省去為每個(gè)<fmt:message>標(biāo)簽指定資源束的很多步驟。
舉例來(lái)說(shuō),下面的兩個(gè)<fmt:bundle>塊將產(chǎn)生同樣的輸出:
<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>
語(yǔ)法格式
<fmt:bundle baseName="<string>" prefix="<string>"/>
屬性
<fmt:bundle>標(biāo)簽有如下屬性:
屬性 | 描述 | 是否必要 | 默認(rèn)值 |
---|---|---|---|
basename | 指定被載入的資源束的基礎(chǔ)名稱 | 是 | 無(wú) |
prefix | 指定<fmt:message>標(biāo)簽key屬性的前綴 | 否 | 無(wú) |
程序示例
資源束包含區(qū)域特定對(duì)象。資源束包含鍵值對(duì)。當(dāng)您的程序需要區(qū)域特定資源時(shí),可以將所有的關(guān)鍵詞對(duì)所有的locale共享,但是也可以為locale指定轉(zhuǎn)換后的值。資源束可以幫助提供指定給locale的內(nèi)容。
一個(gè)Java資源束文件包含一系列的鍵值對(duì)。我們所關(guān)注的方法涉及到創(chuàng)建繼承自java.util.ListResourceBundle 類的已編譯Java類。您必須編譯這些類然后放在您的Web應(yīng)用程序的CLASSPATH中。
讓我們來(lái)定義一個(gè)默認(rèn)的資源束:
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應(yīng)用程序的CLASSPATH能找到的地方?,F(xiàn)在可以使用JSTL來(lái)顯示這三個(gè)數(shù)字了,就像這樣:
<%@ 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 標(biāo)簽</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>
運(yùn)行結(jié)果如下:
One Two Three
將其改為無(wú)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 標(biāo)簽</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>
運(yùn)行結(jié)果如下:
One Two Three
可以查看<fmt:setLocale>和<fmt:setBundle>來(lái)獲取更多信息。
- JSP 教程
- JSP 簡(jiǎn)介
- JSP 開(kāi)發(fā)環(huán)境搭建
- JSP 結(jié)構(gòu)
- JSP 語(yǔ)法
- JSP 指令
- JSP 動(dòng)作元素
- JSP 隱式對(duì)象
- JSP 服務(wù)器響應(yīng)
- JSP 表單處理
- JSP 過(guò)濾器
- JSP Session
- JSP 文件上傳
- JSP 頁(yè)面重定向
- JSP 點(diǎn)擊量統(tǒng)計(jì)
- JSP 自動(dòng)刷新
- JSP 標(biāo)準(zhǔn)標(biāo)簽庫(kù)(JSTL)
- JSP XML 數(shù)據(jù)處理
- JSP 異常處理
- JSP 調(diào)試