“Jstl format bundle tag”的版本间差异

本页内容
(创建页面,内容为“{{DISPLAYTITLE:<fmt:bundle>}}19 = <fmt:bundle> 标签 = JSP 标准标签库 <fmt:bundle>标签将指定的资源束对出现在<fmt:bundle>标签中的<fmt:message>标签可用。这可以使您省去为每个<fmt:message>标签指定资源束的很多步骤。 举例来说,下面的两个<fmt:bundle>块将产生同样的输出: <pre> <fmt:bundle basename="com.xiaobai.Example…”)
 
Neo讨论 | 贡献
无编辑摘要
 
(未显示同一用户的1个中间版本)
第8行: 第8行:
举例来说,下面的两个&lt;fmt:bundle&gt;块将产生同样的输出:
举例来说,下面的两个&lt;fmt:bundle&gt;块将产生同样的输出:


<pre>
<sample title="" desc="" lang="jsp" hererun="1">
&lt;fmt:bundle basename=&quot;com.xiaobai.Example&quot;&gt;
<fmt:bundle basename="com.xiaobai.Example">
     &lt;fmt:message key=&quot;count.one&quot;/&gt;
     <fmt:message key="count.one"/>
&lt;/fmt:bundle&gt;
</fmt:bundle>


&lt;fmt:bundle basename=&quot;com.xiaobai.Example&quot; prefix=&quot;count.&quot;&gt;
<fmt:bundle basename="com.xiaobai.Example" prefix="count.">
     &lt;fmt:message key=&quot;title&quot;/&gt;
     <fmt:message key="title"/>
&lt;/fmt:bundle&gt;
</fmt:bundle>
</pre>
</sample>
=== 语法格式 ===
=== 语法格式 ===


<pre>&lt;fmt:bundle baseName=&quot;&lt;string&gt;&quot; prefix=&quot;&lt;string&gt;&quot;/&gt;</pre>
<sample title="" desc="" lang="jsp" hererun="1"> <fmt:bundle baseName="<string>" prefix="<string>"/> </sample>
=== 属性 ===
=== 属性 ===


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


<pre>
<sample title="" desc="" lang="jsp" hererun="1">
package com.xiaobai;
package com.xiaobai;


第59行: 第59行:
   }
   }
   static final Object[][] contents = {
   static final Object[][] contents = {
   {&quot;count.one&quot;, &quot;One&quot;},
   {"count.one", "One"},
   {&quot;count.two&quot;, &quot;Two&quot;},
   {"count.two", "Two"},
   {&quot;count.three&quot;, &quot;Three&quot;},
   {"count.three", "Three"},
   };
   };
}
}
</pre>
</sample>
编译以上文件为 Example.class,然后放在Web应用程序的CLASSPATH能找到的地方。现在可以使用JSTL来显示这三个数字了,就像这样:
编译以上文件为 Example.class,然后放在Web应用程序的CLASSPATH能找到的地方。现在可以使用JSTL来显示这三个数字了,就像这样:


<pre>
<sample title="" desc="" lang="jsp" hererun="1">
&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;
<%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding=&quot;UTF-8&quot;%&gt;
     pageEncoding="UTF-8"%>
&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot; %&gt;
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/fmt&quot; prefix=&quot;fmt&quot; %&gt;
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
&lt;html&gt;
<html>
&lt;head&gt;
<head>
&lt;title&gt;JSTL fmt:bundle 标签&lt;/title&gt;
<title>JSTL fmt:bundle 标签</title>
&lt;/head&gt;
</head>
&lt;body&gt;
<body>


&lt;fmt:bundle basename=&quot;com.xiaobai.Example&quot; prefix=&quot;count.&quot;&gt;
<fmt:bundle basename="com.xiaobai.Example" prefix="count.">
   &lt;fmt:message key=&quot;one&quot;/&gt;&lt;br/&gt;
   <fmt:message key="one"/><br/>
   &lt;fmt:message key=&quot;two&quot;/&gt;&lt;br/&gt;
   <fmt:message key="two"/><br/>
   &lt;fmt:message key=&quot;three&quot;/&gt;&lt;br/&gt;
   <fmt:message key="three"/><br/>
&lt;/fmt:bundle&gt;
</fmt:bundle>


&lt;/body&gt;
</body>
&lt;/html&gt;
</html>
</pre>
</sample>
运行结果如下:
运行结果如下:


<pre>
<sample title="" desc="" lang="jsp" hererun="1">
One
One
Two
Two
Three
Three
</pre>
</sample>
将其改为无prefix属性:
将其改为无prefix属性:


<pre>
<sample title="" desc="" lang="jsp" hererun="1">
&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;
<%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding=&quot;UTF-8&quot;%&gt;
     pageEncoding="UTF-8"%>
&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot; %&gt;
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/fmt&quot; prefix=&quot;fmt&quot; %&gt;
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
&lt;html&gt;
<html>
&lt;head&gt;
<head>
&lt;title&gt;JSTL fmt:bundle 标签&lt;/title&gt;
<title>JSTL fmt:bundle 标签</title>
&lt;/head&gt;
</head>
&lt;body&gt;
<body>


&lt;fmt:bundle basename=&quot;com.xiaobai.Example&quot;&gt;
<fmt:bundle basename="com.xiaobai.Example">
   &lt;fmt:message key=&quot;count.one&quot;/&gt;&lt;br/&gt;
   <fmt:message key="count.one"/><br/>
   &lt;fmt:message key=&quot;count.two&quot;/&gt;&lt;br/&gt;
   <fmt:message key="count.two"/><br/>
   &lt;fmt:message key=&quot;count.three&quot;/&gt;&lt;br/&gt;
   <fmt:message key="count.three"/><br/>
&lt;/fmt:bundle&gt;
</fmt:bundle>


&lt;/body&gt;
</body>
&lt;/html&gt;
</html>
</pre>
</sample>
运行结果如下:
运行结果如下:


<pre>
<sample title="" desc="" lang="jsp" hererun="1">
One
One
Two
Two
Three
Three
</pre>
</sample>
可以查看[[jstl format setlocale tag|&lt;fmt:setLocale&gt;]]和[[jstl format setbundle tag|&lt;fmt:setBundle&gt;]]来获取更多信息。
可以查看[[jstl format setlocale tag|&lt;fmt:setLocale&gt;]]和[[jstl format setbundle tag|&lt;fmt:setBundle&gt;]]来获取更多信息。


[[jsp jstl|JSP 标准标签库]]
[[jsp jstl|JSP 标准标签库]]

2022年8月17日 (三) 20:25的最新版本

<fmt:bundle> 标签

JSP 标准标签库

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

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


示例

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

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

语法格式

示例

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

属性

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

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

程序示例

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

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

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


示例

package com.xiaobai;

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来显示这三个数字了,就像这样:


示例

<%@ 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.xiaobai.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.xiaobai.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 标准标签库

此页面最后编辑于2022年8月17日 (星期三) 20:25。