Jsp-jstl-format-bundle-tag

提供:Dev Guides
移動先:案内検索

JSTL-コア<fmt:bundle>タグ

*<fmt:bundle>* タグは、bou * *nding <fmt:bundle>* *と *</fmt:bundle>* タグの間にあるすべての *<fmt:message>* タグで指定されたバンドルを利用可能にします。 。 これにより、 *<fmt:message>* タグごとにリソースバンドルを指定する必要がなくなります。

たとえば、次の2つの<fmt:bundle>ブロックは同じ出力を生成します-

<fmt:bundle basename = "com.finddevguides.Example">
   <fmt:message key = "count.one"/>
</fmt:bundle>

<fmt:bundle basename = "com.finddevguides.Example" prefix = "count.">
   <fmt:message key = "title"/>
</fmt:bundle>

属性

*<fmt:bundle>* タグには次の属性があります-
Attribute Description Required Default
basename Specifies the base name of the resource bundle that is to be loaded. Yes None
Prefix Value to prepend to each key name in <fmt:message> subtags No None

リソースバンドルには、ロケール固有のオブジェクトが含まれています。 リソースバンドルには*キー/値*のペアが含まれます。 プログラムにロケール固有のリソースが必要な場合、すべてのロケールに共通するすべてのキーを保持しますが、ロケール固有の翻訳された値を持つことができます。 リソースバンドルは、特定のコンテンツをロケールに提供するのに役立ちます。

Javaリソースバンドルファイルには、一連の*キーから文字列へのマッピング*が含まれています。 私たちが焦点を当てる方法は、 java.util.ListResourceBundle クラスを拡張するコンパイル済みJavaクラスの作成を伴います。 これらのクラスファイルをコンパイルし、Webアプリケーションのクラスパスで使用できるようにする必要があります。

次のようにデフォルトのリソースバンドルを定義しましょう-

package com.finddevguides;

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つの数字を表示できます-

<%@ 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 Tag</title>
   </head>

   <body>
      <fmt:bundle basename = "com.finddevguides.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

次のようにプレフィックスなしで上記の例を試してください-

<%@ 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 Tag</title>
   </head>

   <body>
      <fmt:bundle basename = "com.finddevguides.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

link:/jsp/jstl_format_setlocale_tag [<fmt:setLocale>]およびlink:/jsp/jstl_format_setbundle_tag [<setBundle>]タグを確認して、完全な概念を理解してください。