首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

JSP 國際化:使用 JSTL 格式化貨幣與日期的完整指南

     編輯:Java 相關知識

前言

在開發面向全球用戶的 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/>

進階:同一 Locale 下顯示不同貨幣

使用 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> 標籤支援豐富屬性,可精確控制輸出樣式。

核心屬性速查表

屬性可選值說明
typetime, date, both控制輸出時間、日期或兩者
dateStyleshort, medium, long, full (default)日期具體格式
timeStyleshort, medium, long, full (default)時間具體格式
valuejava.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>

四、最佳實踐建議

  • 統一時區策略:建議在 Filter 或 Controller 層統一設定用戶時區,避免頁面散落 <fmt:timeZone>。
  • 資源檔管理:將 Locale 相關配置(如默認貨幣、日期風格)集中在屬性檔,便於維護。
  • 測試覆蓋:針對目標市場的主流 Locale(en_US, zh_CN, ja_JP, ar_EG 等)進行實機測試,特別注意 RTL 語言的排版。
  • 性能考量:<fmt:setLocale> 僅影響當前頁面執行緒,無全域副作用,可放心在頁面多處切換。

五、總結

JSTL fmt 標籤庫以聲明式方式解決了貨幣與日期的國際化顯示難題,開發者無需手寫格式化邏輯,即可獲得符合各地區慣例的專業級輸出。掌握 formatNumber 與 formatDate 的核心屬性組合,配合 setLocale 動態切換,即可輕鬆構建真正面向全球用戶的 Web 應用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved