<fmt:bundle> タグ

JSP 标准标签库JSP 標準タグライブラリ

<fmt:bundle>タグは、指定されたリソースバンドルを <fmt:bundle> タグ内に現れる <fmt:message> タグで利用可能にします。これにより、各 <fmt:message> タグにリソースバンドルを指定する多くの手順を省くことができます。

例えば、以下の2つの <fmt:bundle> ブロックは同じ出力を生成します:

<fmt:bundle basename="com.サンプル.Example">
    <fmt:message key="count.one"/>
</fmt:bundle>

<fmt:bundle basename="com.サンプル.Example" prefix="count.">
    <fmt:message key="title"/>
</fmt:bundle>

構文形式

<fmt:bundle baseName="<string>" prefix="<string>"/>

属性

<fmt:bundle> タグには以下の属性があります:

属性 説明 必須かどうか デフォルト値
basename ロードされるリソースバンドルの基本名を指定します である 无
prefix <fmt:message> タグの key 属性のプレフィックスを指定します 否 无


プログラム例

リソースバンドルにはロケール固有のオブジェクトが含まれます。リソースバンドルにはキーと値のペアが含まれます。プログラムがロケール固有のリソースを必要とする場合、すべてのキーをすべてのロケールで共有できますが、ロケールごとに変換された値を指定することもできます。リソースバンドルは、ロケールに固有のコンテンツを提供するのに役立ちます。

Java リソースバンドルファイルには一連のキーと値のペアが含まれます。ここで注目する方法は、java.util.ListResourceBundle クラスを継承するコンパイル済み Java クラスを作成することです。これらのクラスをコンパイルし、Web アプリケーションの CLASSPATH に配置する必要があります。

デフォルトのリソースバンドルを定義してみましょう:

package com.サンプル;

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 を使用してこれら3つの数値を表示できます:

<%@ 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.サンプル.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.サンプル.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>詳細情報を取得できます。


JSP 标准标签库JSP 標準タグライブラリ

その他の拡張