Recipe 23.6 Using the Formatting JSTL Tags


Problem

You want to format a date or a number using the JSTL.

Solution

Use the fmt:formatDate and fmt:formatNumber actions.

Discussion

Internationalization or "i18n" is the process by which web developers design their web sites to accommodate visitors who use different languages.

The term "i18n" means internationalization begins with "i," is followed by 18 letters , and ends with "n." It is designed to relieve the tedium of spelling out the word several times.


Localization means adding specific resources to a web site to enable messages such as web page greetings to be translated into the visitor's language. For example, you might localize a site for Japanese visitors by adding resources that contain Japanese translations of text that appears on web pages (I cover more i18n- related Java code in Chapter 24). Example 23-6 uses the JSTL formatting tag library to display the current date and a large number in Swiss and U.S. styles.

Example 23-6. showing a date and a number for U.S. and Swiss audiences
  <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <%--include this taglib for i18n related actions --%> <%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>  <html> <head><title>Formatting numbers and dates</title></head> <body> <h2>Dates and numbers in Swiss and US style formats</h2>  <%-- create an object representing the current date --%> <jsp:useBean id="now" class="java.util.Date"/> <%-- set the locale to German language, Swiss country code --%> <fmt:setLocale value="de_CH"/>  <strong>Swiss-style date:</strong>  <%-- output the date --%> <fmt:formatDate type=   "both" value="${now}" dateStyle="full" timeStyle="short" />  <br /> <strong>Swiss-style number:</strong>  <%-- output the equivalent of java.util.Date.getTime( ) to show how numbers are formatted --%> <fmt:formatNumber value="${now.time}" />  <br /><br />  <%-- reset the locale to English language, US country code --%> <fmt:setLocale value="en_US"/>  <strong>US-style date:</strong>  <%-- output the date --%> <fmt:formatDate type="both" value="${now}" dateStyle=   "full" timeStyle="short" />  <br /> <strong>US-style number:</strong>  <fmt:formatNumber value="${now.time}" />  <br /><br /> </body> </html> 

Example 23-6 uses fmt:setLocale to set the context for formatting dates and numbers first to Swiss-German ("de_CH"), then back to U.S. English ("en_US").

A "locale" represents a particular cultural, geographic, or political region. Locales are typically specified by a String showing a language code followed by an underscore "_" and the country code. See Chapter 24 for an expanded discussion of i18n topics.


Both fmt:formateDate and fmt:formatNumber use the current locale to format their information. The fmt:formateDate tag has several attributes that are designed to configure the date format. The both attribute specifies whether to output only the date, the time, or both the date and time, as in Example 23-6. The dateStyle and timeStyle attributes have settings that derive from the java.text.DateFormat class. Example 23-6 specifies a "full" date display that includes the day of week, the month, and the year. The code also specifies a "short" time display (such as "8:07").

Figure 23-4 shows the JSP that formats dates and numbers for Swiss-German and U.S. English speakers . There are several other formatting related JSTL tags, which the recipes of Chapter 24 cover in more detail.

Figure 23-4. The fmt:formatDate and fmt:formateNumber tags perform translation magic in a JSP
figs/jsjc_2304.gif

See Also

Chapter 24 on using the JSTL's several i18n-related tags; the Jakarta Project's Taglibs site: http://jakarta.apache.org/ taglibs /index.html; the Sun Microsystems JSTL information page: http://java.sun.com/products/jsp/jstl/; Recipe 23.3 on using the core tags; Recipe 23.5 on using the XML Transform tags; Recipe 23.7 and Recipe 23.8 on the JSTL's SQL features; Recipe 23.9-Recipe 23.14 on using the EL to access scoped variables , cookies, and JavaBean properties.



Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net