1. 引言
在本教程中,我們將學習如何使用 ThymeLeaf(https://www.thymeleaf.org/)根據區域設置格式化貨幣。
2. Maven 依賴
讓我們首先導入 Spring Boot Thymeleaf 依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>3.1.5</version>
</dependency>3. 項目設置
我們的項目將是一個簡單的 Spring Web 應用程序,它會根據用户的區域設置來顯示貨幣。 讓我們在 resources/templates/currencies 目錄下創建我們的 Thymeleaf 模板,名為 currencies.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Currency table</title>
</head>
</html>我們還可以創建一個控制器類,用於處理我們的請求:
@Controller
public class CurrenciesController {
@GetMapping(value = "/currency")
public String exchange(
@RequestParam(value = "amount") String amount, Locale locale) {
return "currencies/currencies";
}
}4. 格式化
當處理貨幣時,我們需要根據請求者的地區設置進行格式化。
在這種情況下,我們將使用 Accept-Language 標頭與每個請求一起發送,以代表用户的地區設置。
4.1. 貨幣
Thymeleaf 提供的 Numbers 類支持貨幣格式化。因此,我們更新視圖時,調用 formatCurrency 方法。
<p th:text="${#numbers.formatCurrency(param.amount)}"></p>當我們運行我們的示例時,我們會看到貨幣格式正確:
@Test
public void whenCallCurrencyWithUSALocale_ThenReturnProperCurrency() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/currency")
.header("Accept-Language", "en-US")
.param("amount", "10032.5"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("$10,032.50")));
}由於我們設置了 Accept-Language 請求頭為美國,因此貨幣格式使用小數點和美元符號。
4.2. 貨幣數組
我們可以使用 Numbers 類來格式化數組。 結果,我們將在控制器中添加另一個請求參數:
@GetMapping(value = "/currency")
public String exchange(
@RequestParam(value = "amount") String amount,
@RequestParam(value = "amountList") List amountList, Locale locale) {
return "currencies/currencies";
}接下來,我們可以更新我們的視圖,其中包含對 listFormatCurrency 方法的調用:
<p th:text="${#numbers.listFormatCurrency(param.amountList)}"></p>
現在讓我們看看結果是什麼樣的:
@Test
public void whenCallCurrencyWithUkLocaleWithArrays_ThenReturnLocaleCurrencies() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/currency")
.header("Accept-Language", "en-GB")
.param("amountList", "10", "20", "30"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("£10.00, £20.00, £30.00")));
}結果顯示了包含正確英國格式的貨幣列表。
4.3. 尾隨零的處理
使用 <a href="https://www.thymeleaf.org/apidocs/thymeleaf/3.0.11.RELEASE/org/thymeleaf/expression/Strings.html"><em>Strings#replace</em></a>, 我們可以移除尾隨零。
<p th:text="${#strings.replace(#numbers.formatCurrency(param.amount), '.00', '')}"></p>現在我們可以看到完整的金額,不再有尾隨的雙零:
@Test
public void whenCallCurrencyWithUSALocaleWithoutDecimal_ThenReturnCurrencyWithoutTrailingZeros()
throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/currency")
.header("Accept-Language", "en-US")
.param("amount", "10032"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("$10,032")));
}4.4. 小數
根據地區設置,小數的格式可能不同。因此,如果我們要用逗號替換小數點,可以使用 <em>Numbers</em> 類中提供的 <em>formatDecimal</em> 方法:
<p th:text="${#numbers.formatDecimal(param.amount, 1, 2, 'COMMA')}"></p>讓我們看看測試結果:
@Test
public void whenCallCurrencyWithUSALocale_ThenReturnReplacedDecimalPoint() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/currency")
.header("Accept-Language", "en-US")
.param("amount", "1.5"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("1,5")));
}值將格式化為“1,5”。
5. 結論
在本簡短教程中,我們展示瞭如何使用 Thymeleaf 與 Spring Web 結合,利用用户的 Locale 處理貨幣。