Reproduced from
Click to open the link
description of FMT in JSTL Blog Categories: JSP Jstl FMT
See i18n should think of knowledge "internationalization", i18n formatting tag library is used in JSP pages to do the internationalization of the action. There are 12 labels in the tag library, divided into two categories, respectively:
Q Internationalization Core Tags: <fmt:setLocale>, <fmt:bundle>, <fmt:setBundle>, <fmt:message>, <fmt:para M>, <fmt:requestEncoding>.
Q Format Tags: <fmt:timeZone>, <fmt:setTimeZone>, <fmt:formatNumber>, <fmt:parseNumber>, ;fmt:formatdate>, <fmt:parseDate>.
Here are just a few of the common tags and properties to introduce.
9.5.1 <fmt:setLocale> label for setting up a localized environment
<fmt:setLocale> tags are used to set up Locale environments. Its properties and descriptions are shown in table 9.17:
Table 9.17 <fmt:setLocale> label properties and descriptions
Property |
Describe |
Value |
Locale environment, can be an instance of Java.util.Locale or String type |
Scope |
Locale the scope of the environment variable (optional) |
Let's look at an example:
<fmt:setlocale value= "Zh_tw"/>
Indicates that the local environment is set to traditional Chinese.
9.5.2 <fmt:bundle>, <fmt:setBundle> tags for resource file bindings
The two sets of tags are used for the binding of the resource profile, except that the <fmt:bundle> tag binds the resource profile to the display in its label body, and the <fmt:setBundle> tag allows the resource profile to be saved as a variable. The following work can be done according to the variable.
Depending on the Locale environment, you will find resource profiles with different suffixes, which are consistent on any technology in the international context, and generally do not make sense for the two labels to be used alone, and are used in conjunction with other tags in the i18n formatting tag library. Their properties and descriptions are shown in table 9.18:
Table 9.18 <fmt:bundle>, <fmt:setBundle> label properties and descriptions
Property |
Describe |
BaseName |
A resource configuration file is specified by simply specifying the file name without the extension, and the properties shared by the two sets of labels |
Var |
<fmt:setBundle> unique properties for saving a resource profile as a variable |
Scope |
Scope of the variable |
Let's look at an example
<fmt:setlocale value= "Zh_cn"/>
<fmt:setbundle basename= "Applicationmessage" var= "Applicationbundle"/>
The example will look for a resource profile named Applicationmessage_zh_cn.properties to be the Resource binding for the display.
9.5.3 <fmt:message> label for displaying resource profile information
The label used for the display of the information, which displays the information defined in the resource configuration file. Its properties and descriptions are shown in table 9.19:
Table 9.19 <fmt:message> label properties and descriptions
Property |
Describe |
Key |
"Key" designation of the resource configuration file |
Bundle |
If you use <fmt:setBundle> to save a resource profile, this property can be found from a saved resource profile |
Var |
Save display information as a variable |
Scope |
Scope of the variable |
Let's look at an example:
<fmt:setbundle basename= "Applicationmessage" var= "Applicationbundle"/>
<fmt:bundle basename= "Applicationallmessage" >
<fmt:message key= "UserName"/>
<p>
<fmt:message key= "PassWord" bundle= "${applicationbundle}"/>
</fmt:bundle>
The example uses the binding of two resource profiles, the "Applicationmessage" resource profile is assigned to the variable "Applicationbundle" using the <fmt:setBundle> tag, and as <FMT: The "Applicationallmessage" resource profile defined by the Bundle> label acts on the display in its label body.
Q The first <fmt:message> label will be displayed using the information "key" in the "Applicationallmessage" Resource configuration file as "UserName".
Q The second <fmt:message> tag is defined in the <fmt:bundle> tag body, but it uses the bundle property, so it will specify the previously saved by the <fmt:setBundle> tag " Applicationmessage resource configuration file, the key is displayed for "PassWord" information.
9.5.4 <fmt:param> label for parameter passing
The <fmt:param> label should be in the <fmt:message> tab and will provide parameter values for the message label. It has only one attribute value.
<fmt:param> labels are available in two versions, one is to write the parameter values directly in the Value property, and the other is to write the parameter values in the label body.
9.5.6 <fmt:requestEncoding> label used to set character encoding for a request
The <fmt:requestEncoding> label is used to set the character encoding for the request. It has only one property value, in which the character encoding can be defined.
9.5.7 used to set the <fmt:timeZone>, <fmt:setTimeZone> label for the time zone
Both sets of labels are used to set a time zone. The only difference is that the <fmt:timeZone> tag will allow the work in its label body to use the time zone setting, and the <fmt:setBundle> tab allows the time zone setting to be saved as a variable, which can then be performed according to the variable. Their properties and descriptions are shown in table 9.20:
Table 9.20 <fmt:timeZone>, <fmt:setTimeZone> label properties and descriptions
Property |
Describe |
Value |
Settings for time zones |
Var |
<fmt:setTimeZone> Unique property that holds the time zone as a variable |
Scope |
Scope of the variable |
9.5.8 <fmt:formatNumber> label for formatting numbers
<fmt:formatnumber > Labels are used to format numbers. Its properties and descriptions are shown in table 9.21:
Table 9.21 <fmt:formatNumber> label properties and descriptions
Property |
Description |
Value | A number formatted with
That can be an instance of a String type or java.lang.Number type |
Type |
Formatted type |
Pattern |
Format mode |
var |
Result save variable |
Scope |
Scope of variables |
Maxintegerdigits |
Specify the maximum value for the formatted results |
Minintegerdigits |
Specify minimum values for formatted results |
Maxfractiondigits |
To specify the maximum number of formatted results, with decimals |
Minfractiondigits |
Specify the minimum value for the formatted result, with decimals |
<fmt:formatNumber> tags are actually corresponding to the Java.util.NumberFormat class, and the possible values for the type attribute include currency (currency), number (digits), and percent (percent).
Let's look at an example.
<fmt:formatnumber value= "1000.888" type= "Currency" var= "Money"/>
The result will be saved in the " money " variable, and the local currency format will be displayed according to the Locale environment. <fmt:formatnumber value= "A" type= "currency" pattern= "$.00"/> //-- $12.00 <fmt:formatnumber value= "a" type= "currency" pattern= "$.0#"/> //-- $12.0 <fmt:formatnumber value= "1234567890" type= "Currency"/> //-- $1,234,567,890.00 (the symbol for that currency is related to the local settings of the current Web server) <fmt: FormatNumber value= "123456.7891" pattern= "#, #00.0#"/>// -- 123,456.79 <fmt:formatnumber value= "123456.7" pattern= "#, #00.0#"/> //-- 123,456.7 <fmt:formatnumber value= "123456.7" pattern= "#, #00.00#"/>// -- 123,456.70 <fmt:formatnumber value= "A" type= "percent" /> //--1,200% ( type can be currency, number, and PercenT).
9.5.9 <fmt:parseNumber> Label for resolution of numbers
The <fmt:parseNumber> tab is used to parse a number and return the result as an instance of the Java.lang.Number class. The <fmt:parseNumber> label looks just the opposite of what the <fmt:formatNumber> tag does. Its properties and descriptions are shown in table 9.22:
Table 9.22 <fmt:parseNumber> label properties and descriptions
Property |
Describe |
Value |
The string that will be parsed |
Type |
Parsing the formatted type |
Pattern |
Parsing format Patterns |
Var |
Result save variable, type Java.lang.Number |
Scope |
Scope of the variable |
Parselocale |
Resolves a string in a localized form that should be an instance of a string or Java.util.Locale type |
Let's look at an example.
<fmt:parsenumber value= "15%" type= "percent" var= "num"/>
The result after parsing is "0.15".
9.5.10 <fmt:formatDate> label for formatting dates
<fmt:formatDate> labels are used to format dates. Its properties and descriptions are shown in table 9.23:
Table 9.23 <fmt:formatDate> label properties and descriptions
Property |
Describe |
Value |
The formatted date where the content of the property should be an instance of the Java.util.Date type |
Type |
Types of formatting |
Pattern |
Format mode |
Var |
Result Save variable |
Scope |
Scope of the variable |
TimeZone |
Specify a time zone for formatting dates |
<fmt:formatDate> label is closely related to <fmt:timeZone>, <fmt:setTimeZone> two groups of labels. If you do not specify the TimeZone attribute, you can also format the final result by using a time zone set by the <fmt:timeZone>, <fmt:setTimeZone> two sets of labels.
9.5.11 <fmt:parseDate> Label for resolution of dates
The <fmt:parseDate> tab is used to parse a date and return the result as an instance of the Java.lang.Date type. The <fmt:parseDate> label looks just the opposite of what the <fmt:formatDate> tag does. Its properties and descriptions are shown in table 9.24:
Table 9.24 <fmt:parseDate> label properties and descriptions
Property |
Description |
Value |
The string that will be parsed |
Type |
Parsing formatted type |
Pattern |
Parse format mode |
var |
Result save variable, type java.lang.date |
Scope |
Scope of variables |
Parselocale |
Resolves a string in a localized form that has the contents of a String or java.util.Locale type Instance |
TimeZone |
Specify the time zone for resolving a format date |
Both the <fmt:parseNumber> and <fmt:parseDate> tags implement the work of parsing strings as a concrete object instance, so these two sets of parsing tags are very strict with the string parameters of the Var attribute. In the front of the presentation layer of the JSP page, dealing with this kind of parsing is not part of the matter, so <fmt:parseNumber> and <fmt:parseDate> two groups of labels should be used sparingly, alternative work should be in the server-side presentation layer of the backend, For example, in a Servlet.
All Tags:
Tags
Fmt:requestencoding
Fmt:setlocale
Fmt:timezone
Fmt:settimezone
Fmt:bundle
Fmt:setbundle
Fmt:message
Fmt:param
Fmt:formatnumber
Fmt:parsenumber
Fmt:formatdate
Fmt:parsedate
<%@ taglib uri= "http://java.sun.com/jsp/jstl/fmt" prefix= "FMT"%>
Jstl FMT function Daquan
Main feature formatting
Date format (May 5, 2008 22:00 23 seconds)
<fmt:formatdate value= "<%=new date ()%>" pattern= "yyyy mm month dd Day hh dot mm minute ss seconds"/>
Keep two decimal digits
<fmt:formatnumber value= "123.123456789" pattern= "0.00"/>
Format number (45,678.234)
<fmt:formatnumber type= "number" value= "45678.2345"/>
Format percent (23%)
<fmt:formatnumber type= "percent" value= "0.2345"/>
Other
<fmt:bundle>: Resource bindings. This label can also be used in addition to the previously mentioned declarations in Web.xml.
Example <fmt:bundle basename= "message" ></fmt:bundle>
<fmt:setlocale>: Set locale, mainly used in this case, a Chinese in foreign countries, locale is en_us, but want to display in Chinese.
Example: <fmt:setlocal value= "ZH_CN"/>
<FMT:MESSAGE>: Output The specified content in the properties file.
Example <fmt:message key= "user"/>
<fmt:formatnumber type= "number" > Format ordinary Numbers
<fmt:formatnumber type= "percent" > Format percent
Three types of numeric parameters: Currency,number,percent
<fmt:parsenumber var= "I" type= "number" value= "45678.2345"/>
<c:out value= "${i}" escapexml= "false"/> Parse number
<fmt:requestencoding value= "GB18030"/> Formatted text encoding
<fmt:formatdate value= "${date}" type= "both" timestyle= "long" datestyle= "Long"/>
Type= "Both" Input date and also output specific time
Timestyle= "Long" time output difference: 02:06 P.M. 59 seconds and 14:06:59
datestyle= "Long" date output difference: September 7, 2006 and 2006-9-7
Four length parameters: Long,short,medium,full
<fmt:timezone value= "${timezone}"/> Time zone offset, which can be used in conjunction with the above:
<fmt:formatdate value= "${d}" timezone= "${zn}" type= "both"/>
<fmt:parsedate var= "I" type= "date" value= "2006-12-11"/>
<c:out value= "${i}" escapexml= "false"/> Parse out time
A more detailed explanation:
<fmt:setLocale> tags are used for a given locale to store in a zone configuration variable. Properties:
<fmt:setLocale> the same label has the following properties:
Property |
Description |
must be |
default |
Value |
Specifies a Two-part code that represents the ISO-639 language code and a ISO-3166 code. |
Yes |
en_US |
Variant |
Browser-specific Variant |
No |
None |
Scope |
Scope of the locale configuration variable |
No |
Page |
Example:
A resource bundle contains locale-specific objects. The resource bundle contains key/value pairs. When a program needs locale-specific resources, put all the keys in a common language environment, but can have a specific locale converted to the value. Resource bundles help provide a specific locale for content.
A Java resource bundle file contains a series of key to string mappings. Our focus on this approach involves creating a compiled Java class to extend the Java.util.ListResourceBundle class. You must compile these class files and provide them for use in the classpath of the Web application.
Let's define a default resource bundle, as follows:
Package Com.yiibai;
Import Java.util.ListResourceBundle;
public class Example_en extends ListResourceBundle {public
object[][] getcontents () {return
contents;
}
static final object[][] Contents = {