<fmt:setBundle>
標籤用於加載資源包,並將其存儲在命名作用域變數或包配置變數中。
屬性
<fmt:setBundle>
標籤具有以下屬性 -
屬性 | 描述 | 必需 | 默認 |
---|---|---|---|
basename |
作為範圍或配置變數公開的資源綁定包系列的基本名稱 | 是 | en_US |
var |
用於存儲新捆綁包的變數的名稱 | 否 | 替換默認值 |
scope |
存儲新捆綁包的變數的範圍 | 否 | page |
示例
下麵定義一個資源捆綁包如下 -
檔: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", "三個" }, };
}
下麵是顯示JSP頁面的代碼 -
檔:fmt_setLocale.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:setBundle標籤</title>
</head>
<body>
<fmt:setLocale value="zh_cn" />
<fmt:setBundle basename="com.zaixian.Example_Cn" var="lang" />
<fmt:message key="count.one" bundle="${lang}" />
<br />
<fmt:message key="count.two" bundle="${lang}" />
<br />
<fmt:message key="count.three" bundle="${lang}" />
<br />
</body>
</html>
編譯上述的Example_Cn
,代碼運行結果如下 -
上一篇:
Jstl教學
下一篇:
JSP+MySQL實例