ตัวอย่างเช่น สองบล็อก
<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>
คุณสมบัติ
| แอตทริบิวต์ | คำอธิบาย | จำเป็น | ค่าเริ่มต้น |
|---|---|---|---|
| ชื่อฐาน | ระบุชื่อฐานของกลุ่มทรัพยากรที่จะโหลด | ใช่ | ไม่มี |
| คำนำหน้า | ค่าที่จะเติมหน้าชื่อคีย์แต่ละชื่อในแท็กย่อย | ไม่ | ไม่มี |
ตัวอย่าง
บันเดิลรีซอร์สมีอ็อบเจ็กต์เฉพาะโลแคล ชุดทรัพยากรประกอบด้วย คีย์/ค่า คู่ เมื่อโปรแกรมของคุณต้องการทรัพยากรเฉพาะของโลแคล คุณต้องเก็บคีย์ทั้งหมดไว้สำหรับโลแคลทั้งหมด แต่คุณสามารถมีค่าแปลเฉพาะของโลแคลได้ ชุดทรัพยากรช่วยในการจัดเตรียมเนื้อหาเฉพาะให้กับสถานที่
ไฟล์บันเดิลทรัพยากร Java มีชุดของ การแมปคีย์กับสตริง . วิธีการที่เรามุ่งเน้นเกี่ยวข้องกับการสร้างคลาส Java ที่คอมไพล์แล้วซึ่งขยาย java.util.ListResourceBundle ระดับ. คุณต้องรวบรวมไฟล์คลาสเหล่านี้และทำให้พร้อมใช้งานใน classpath ของเว็บแอปพลิเคชันของคุณ
ให้เรากำหนดชุดทรัพยากรเริ่มต้นดังนี้ -
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"},
};
} ให้เรารวบรวมคลาสข้างต้น Example.class และทำให้พร้อมใช้งานใน CLASSPATH ของเว็บแอปพลิเคชันของคุณ ตอนนี้คุณสามารถใช้แท็ก JSTL ต่อไปนี้เพื่อแสดงตัวเลขสามตัวดังนี้ -
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %> <%@ taglib uri = "http://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>
รหัสข้างต้นจะสร้างผลลัพธ์ดังต่อไปนี้ -
One Two Three
ลองใช้ตัวอย่างข้างต้นโดยไม่มีคำนำหน้าดังนี้ −
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %> <%@ taglib uri = "http://java.sun.com/jsp/jstl/fmt" prefix = "fmt" %> <html> <head> <title>JSTL fmt:bundle Tag</title> </head> <body> <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> </body> </html>
รหัสข้างต้นจะสร้างผลลัพธ์ดังต่อไปนี้ -
One Two Three