Tag <fmt:bundle> làm cho bundle chỉ định khả dụng cho tất cả tag <fmt:message> nằm giữa <fmt:bundle> và </fmt:bundle>. Nhờ đó không cần chỉ định resource bundle cho mỗi <fmt:message>.
Hai block <fmt:bundle> sau cho cùng kết quả:
<fmt:bundle basename = "com.tutorialspoint.Example"> <fmt:message key = "count.one"/> </fmt:bundle> <fmt:bundle basename = "com.tutorialspoint.Example" prefix = "count."> <fmt:message key = "title"/> </fmt:bundle>
Thuộc tính
| Attribute | Description | Required | Default |
|---|---|---|---|
| basename | Tên cơ sở resource bundle cần load | Yes | None |
| prefix | Giá trị tiền tố cho key trong <fmt:message> con | No | None |
Ví dụ
Resource bundle chứa đối tượng locale-specific, cặp key/value. Keys chung cho mọi locale, value dịch theo locale. Giúp cung cấp nội dung riêng cho từng locale.
File resource bundle Java chứa ánh xạ key-to-string. Cách làm: tạo class extends java.util.ListResourceBundle, biên dịch và đặt vào classpath web app.
Default resource bundle
package com.tutorialspoint;
import java.util.ListResourceBundle;
public class Example_En extends ListResourceBundle {
public Object[][] getContents() { return contents; }
static final Object[][] contents = {
{"count.one", "One"},
{"count.two", "Two"},
{"count.three", "Three"}
};
}
Biên dịch Example.class đặt vào CLASSPATH. Dùng JSTL hiển thị:
<%@ taglib uri = "https://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%@ taglib uri = "https://java.sun.com/jsp/jstl/fmt" prefix = "fmt" %>
<html>
<head> <title>JSTL fmt:bundle Tag</title> </head>
<body>
<fmt:bundle basename = "com.tutorialspoint.Example" prefix = "count.">
<fmt:message key = "one" /><br/>
<fmt:message key = "two" /><br/>
<fmt:message key = "three" /><br/>
</fmt:bundle>
</body>
</html>
Kết quả: One Two Three
Không dùng prefix:
<fmt:bundle basename = "com.tutorialspoint.Example"> <fmt:message key = "count.one" /><br/> <fmt:message key = "count.two" /><br/> <fmt:message key = "count.three" /><br/> </fmt:bundle>
Kết quả: One Two Three