如何设置定位以在JSP中标识所需的资源包?
<FMT:的setLocale>标记用于给定区域存储在区域设置配置变量。
属性
<FMT:的setLocale>标签具有以下属性-
示例
资源束包含特定于语言环境的对象。资源束包含键/值对。当您的程序需要特定于语言环境的资源时,您可以保留所有语言环境所共有的所有键,但是可以转换特定于语言环境的值。资源束有助于提供特定于语言环境的内容。
Java资源包文件包含一系列键到字符串的映射。我们关注的方法涉及创建扩展java.util.ListResourceBundle类的已编译Java类。您必须编译这些类文件,并使它们可用于Web应用程序的类路径。
让我们定义一个默认资源束,如下所示:
package com.nhooo;
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"},
};
}现在让我们定义另一个资源包,将其用于西班牙文语言环境-
package com.nhooo;
import java.util.ListResourceBundle;
public class Example_es_ES extends ListResourceBundle {
public Object[][] getContents() {
return contents;
}
static final Object[][] contents = {
{"count.one", "Uno"},
{"count.two", "Dos"},
{"count.three", "Tres"},
};
}让我们编译上面的类Example.class和Example_es_ES.class,并使它们在Web应用程序的CLASSPATH中可用。您现在可以使用以下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:setLocale Tag</title>
</head>
<body>
<fmt:bundle basename = "com.nhooo.Example">
<fmt:message key = "count.one"/><br/>
<fmt:message key = "count.two"/><br/>
<fmt:message key = "count.three"/><br/>
</fmt:bundle>
<!-- Change the Locale -->
<fmt:setLocale value = "es_ES"/>
<fmt:bundle basename = "com.nhooo.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 Uno Dos Tres