<fmt:bundle> 标签


<fmt:bundle>标签将指定的资源束对出现在<fmt:bundle>标签中的<fmt:message>标签可用。这可以使您省去为每个<fmt:message>标签指定资源束的很多步骤。

举例来说,下面的两个<fmt:bundle>块将产生同样的输出:

  1. <fmt:bundle basename="com.tutorialspoint.Example">
  2.     <fmt:message key="count.one"/>
  3. </fmt:bundle>
  4.  
  5. <fmt:bundle basename="com.tutorialspoint.Example" prefix="count.">
  6.     <fmt:message key="title"/>
  7. </fmt:bundle>

属性

<fmt:bundle>标签有如下属性:

属性描述是否必要默认值
basename指定被载入的资源束的基础名称
prefix指定<fmt:message>标签key属性的前缀

程序示例

资源束包含区域特定对象。资源束包含键值对。当您的程序需要区域特定资源时,可以将所有的关键词对所有的locale共享,但是也可以为locale指定转换后的值。资源束可以帮助提供指定给locale的内容。

一个Java资源束文件包含一系列的键值对。我们所关注的方法涉及到创建继承自java.util.ListResourceBundle 类的已编译Java类。您必须编译这些类然后放在您的Web应用程序的CLASSPATH中。

让我们来定义一个默认的资源束:

  1. package com.tutorialspoint;
  2.  
  3. import java.util.ListResourceBundle;
  4.  
  5. public class Example_En extends ListResourceBundle {
  6.   public Object[][] getContents() {
  7.     return contents;
  8.   }
  9.   static final Object[][] contents = {
  10.   {"count.one", "One"},
  11.   {"count.two", "Two"},
  12.   {"count.three", "Three"},
  13.   };
  14. }

编译以上文件为Examble.class,然后放在Web应用程序的CLASSPATH能找到的地方。现在可以使用JSTL来显示这三个数字了,就像这样:

  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
  3. <html>
  4. <head>
  5. <title>JSTL fmt:bundle Tag</title>
  6. </head>
  7. <body>
  8.  
  9. <fmt:bundle basename="com.tutorialspoint.Example" prefix="count.">
  10.    <fmt:message key="one"/><br/>
  11.    <fmt:message key="two"/><br/>
  12.    <fmt:message key="three"/><br/>
  13. </fmt:bundle>
  14.  
  15. </body>
  16. </html>

运行结果如下:

  1. One 
  2. Two 
  3. Three

将其改为无prefix属性:

  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
  3. <html>
  4. <head>
  5. <title>JSTL fmt:bundle Tag</title>
  6. </head>
  7. <body>
  8.  
  9. <fmt:bundle basename="com.tutorialspoint.Example">
  10.    <fmt:message key="count.one"/><br/>
  11.    <fmt:message key="count.two"/><br/>
  12.    <fmt:message key="count.three"/><br/>
  13. </fmt:bundle>
  14.  
  15. </body>
  16. </html>

运行结果如下:

  1. One 
  2. Two 
  3. Three

可以查看<fmt:setLocale>和<fmt:setBundle>来获取更多信息。