JSTL Formatting < fmt:bundle > Tag
In JSTL the <fmt:bundle> tag is mainly used to reference and format messages from the resource bundles. This tag allows us to get localized messages based on the user’s locale.
The main difference between <fmt:setBundle> and <fmt:bundle> is that, the <fmt:setBundle> sets the resource bundle for the entire page whereas the <fmt:bundle> is used when the page and allows more localized messages.
In this article, we will discuss the Syntax and the parameters of this tag. We will also see its additional attributes table and the example demonstrating the implementation.
Syntax
<fmt:bundle basename="resourceBundleName"
prefix = "msg"
/>
Where,
- basename: This attribute is the base name of the resource bundle which is to be set.
- prefix: This attribute is used with the <fmt:message> that specifies the value to be prepended in the key values that each time it should not be provided repeatedly.
Attributes of JSTL <fmt:bundle>Tag
The table describes attributes present in <fmt:bundle> tag with proper description.
Attribute |
Description |
Required |
Default |
---|---|---|---|
basename |
This attribute specifies the base name of the resource bundle which is to be exposed as the scoped or config variable |
Yes |
None |
prefix |
This attribute is used with the <fmt:message<, which prepends a value to the key |
No |
None |
Example of JSTL Formatting <fmt:bundle> Tag
In this example, we will discuss how to create any date and time format using the JSTL tag <fmt:bundle>
Note: Make sure to compile the GeekCategories.java and also assure it is available in the CLASSPATH of your application.
GeekCategories.java
Java
//Java Program Bundle Object package com.w3wiki; import java.util.ListResourceBundle; //Class to manage bundle public class GeekCategories extends ListResourceBundle { //Get-method to fetch bundle content public Object[][] getContents() { return contents; } //Object Array static final Object[][] contents = { { "category.Java" , "Java Programming Language" }, { "category.Algorithms" , "Algorithms" }, { "category.DataStructures" , "Data Structures" } }; } |
bundle.jsp
Ensure both GeekCategories.jsp and bundle.java file must be present in the same directory.
Java
<%@ taglib uri= "http://java.sun.com/jsp/jstl/fmt" prefix= "fmt" %> <html> <head> <title>fmt:bundle Tag</title> </head> <body> <fmt:bundle basename= "com.w3wiki.GeekCategories" prefix= "category." > <p>${category.Java}</p> <p>${category.Algorithms}</p> <p>${category.DataStructures}</p> </fmt:bundle> </body> </html> |
Output:
Tag" height="443" src="/public/files/Output-768.png" width="768">
Explanation of the above Code:
In the above example, we have defined the java class GeekCategories.java which defines the resource bundle that contains the demo content related to w3wiki. Then, in the bundle.jsp file, we have used the <fmt:bundle> tag that is used to refer to the GeekCategories resource bundle wit the basename and prefix value. We are displaying the categories received fro the resource bundle using HTML <p> tag.
Contact Us