在開發面向全球用戶的 Web 應用時,正確處理貨幣與日期的本地化顯示至關重要。不同國家地區對數字、貨幣符號、日期時間格式都有獨特慣例。JSP 標準標籤庫(JSTL)的 fmt 標籤庫提供了強大且易用的國際化格式化功能,無需手寫複雜邏輯即可實現多語言環境下的數據展示。
在 JSP 頁面頂部引入核心與格式化標籤庫:
<%@ page pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="https://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="https://java.sun.com/jsp/jstl/fmt" %>
確保頁面編碼設為 UTF-8,避免多語言字符顯示亂碼。
<fmt:formatNumber> 標籤的 type="currency" 屬性可根據當前 Locale 自動套用對應貨幣格式。搭配 <fmt:setLocale> 即可切換顯示語言環境。
<h1>Currency Formatting and Locales</h1>
<h3>English, Great Britain</h3>
<fmt:setLocale value="en_GB">
<fmt:formatNumber type="currency" value="80000" /><br/>
<h3>English, USA</h3>
<fmt:setLocale value="en_US">
<fmt:formatNumber type="currency" value="80000" /><br/>
<h3>French, France</h3>
<fmt:setLocale value="fr_FR">
<fmt:formatNumber type="currency" value="80000" /><br/>
<h3>Japanese, Japan</h3>
<fmt:setLocale value="ja_JP">
<fmt:formatNumber type="currency" value="80000" /><br/>
<h3>Korean, Korea</h3>
<fmt:setLocale value="ko_KR">
<fmt:formatNumber type="currency" value="80000" /><br/>
<h3>Spanish, Spain</h3>
<fmt:setLocale value="es_ES">
<fmt:formatNumber type="currency" value="80000" /><br/>
<h3>Arabic, Egypt</h3>
<fmt:setLocale value="ar_EG">
<fmt:formatNumber type="currency" value="80000" /><br/>
使用 currencyCode 屬性可強制指定貨幣種類,而非跟隨 Locale 默認貨幣:
<h3>Using Local Numeric Formatting for Different Currency</h3>
<h4>English, Great Britain</h4>
<fmt:setLocale value="en_GB">
<fmt:formatNumber type="currency" value="80000" /><br/> <!-- 顯示 GBP -->
<fmt:formatNumber type="currency" value="80000" currencyCode="EUR" /><br/> <!-- 顯示 EUR 但用英式數字格式 -->
<fmt:formatDate> 標籤支援豐富屬性,可精確控制輸出樣式。
| 屬性 | 可選值 | 說明 |
|---|---|---|
type | time, date, both | 控制輸出時間、日期或兩者 |
dateStyle | short, medium, long, full (default) | 日期具體格式 |
timeStyle | short, medium, long, full (default) | 時間具體格式 |
value | java.util.Date | 待格式化的日期對象 |
timeZone | 時區 ID (如 EST, GMT+8) | 指定解析時區 |
<%@ page pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="https://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="https://java.sun.com/jsp/jstl/fmt" %>
<h1>Date Formatting and Locale</h1>
<fmt:timeZone value="EST">
<jsp:useBean id="currentTime" class="java.util.Date" />
<h3>English, Great Britain</h3>
<fmt:setLocale value="en_GB">
<fmt:formatDate type="both" value="${currentTime}" /><br/>
<h3>English, USA</h3>
<fmt:setLocale value="en_US">
<fmt:formatDate type="both" value="${currentTime}" /><br/>
<h3>French, France</h3>
<fmt:setLocale value="fr_FR">
<fmt:formatDate type="both" value="${currentTime}" /><br/>
<h3>Japanese, Japan</h3>
<fmt:setLocale value="ja_JP">
<fmt:formatDate type="both" value="${currentTime}" /><br/>
<h3>Korean, Korea</h3>
<fmt:setLocale value="ko_KR">
<fmt:formatDate type="both" value="${currentTime}" /><br/>
<h3>Spanish, Spain</h3>
<fmt:setLocale value="es_ES">
<fmt:formatDate type="both" value="${currentTime}" /><br/>
<h3>Arabic, Egypt</h3>
<fmt:setLocale value="ar_EG">
<fmt:formatDate type="both" value="${currentTime}" /><br/>
</jsp:useBean>
</fmt:timeZone>
<fmt:timeZone>。<fmt:setLocale> 僅影響當前頁面執行緒,無全域副作用,可放心在頁面多處切換。JSTL fmt 標籤庫以聲明式方式解決了貨幣與日期的國際化顯示難題,開發者無需手寫格式化邏輯,即可獲得符合各地區慣例的專業級輸出。掌握 formatNumber 與 formatDate 的核心屬性組合,配合 setLocale 動態切換,即可輕鬆構建真正面向全球用戶的 Web 應用。