发布于 2015-08-17 14:46:51 | 96 次阅读 | 评论: 0 | 来源: 网络整理

<fmt:bundle> 标记将指定的软件包中,可在边界<fmt:bundle>之间发生的所有<fmt:message>标记 到</fmt:bundle>标记。这样可以节省不必指定为每个<fmt:message>标记的资源包。

例如,下面的两个<fmt:bundle>块会产生同样的输出:



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

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

属性:

<fmt:bundle>标记具有以下属性:

属性 描述 必须 默认
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类来扩展java.util.ListResourceBundle类。你必须编译这些类文件,并提供给您的Web应用程序的classpath中可用。

让我们定义一个默认的资源包,如下所示:


package com.yiibai;

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应用程序的类路径中。现在,可以使用下面的JSTL标记来显示三个号码如下:


<%@ 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.yiibai.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.yiibai.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> 和<setBundle>标签,了解完整的概念。

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务