JSTL Formatting <fmt:setBundle> Tag
The <fmt:setBundle> tag in JSTL is used to set or load the resource bundle that is to be used in localization. This tag allows us to specify the bundle that contains the localized messages and enables the rendering of content in various languages that are specified by the user.
In this article, we will be discussing the Syntax along with the parameters of this tag. We will also see its additional attributes table and lastly the example demonstrating the actual implementation.
Syntax of <fmt:setBundle> Tag
<fmt:setBundle basename="resourceBundleName"
[var="resultVar"]
[scope="page|request|session|application"]
/>
Parameters
- basename: This is the base name of the resource bundle which is to be set.
- var: This is the variable that is used to store the resource bundle. This is the optional attribute.
- scope: This is used to specify the scope of the variable like page, request, session, and application. This is also an optional attribute.
Attributes of JSTL <fmt:setBundle>Tag
The table provides a description of attributes present in <fmt:setBundle> 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 |
var |
This is the variable used to store the resource bundle. |
No |
None |
scope |
This is used to set the scope of variable as page, request, session, application. |
No |
page |
Example of JSTL Formatting <fmt:setBundle> Tag
In this example we will discuss how to create any date and time format using JSTL tag <fmt:setBundle>
BeginnerBundle.java
Java
//Java Program Bundle Object package com.w3wiki; import java.util.ListResourceBundle; //Class to manage bundle public class BeginnerBundle extends ListResourceBundle { //Get-method to fetch bundle content public Object[][] getContents() { return contents; } //Object Array static final Object[][] contents = { { "course.java" , "Java Programming" }, { "course.python" , "Python Programming" }, { "course.algo" , "Algorithm Design" }, }; } |
Note: Make sure to compile the BeginnerBundle.java and also assure it is available in the CLASSPATH of your application.
setBundle.jsp
Ensure both BeginnerBundle.jsp and setBundle.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:setBundle Tag</title> </head> <body> <fmt:setBundle basename= "com.w3wiki.BeginnerBundle" var= "courses" /> <p>${courses[ 'course.java' ]}</p> <p>${courses[ 'course.python' ]}</p> <p>${courses[ 'course.algo' ]}</p> </body> </html> |
Output:
Explanation of the above Program:
- In the above example, we have used the resource bundle as “BeginnerBundle“
- Then we have used the <fmt:setBundle> tag to set the resource bundle and also store it in the variable names as “courses“
- We are displaying this course names using “${courses[‘course.java’]}, ${courses[‘course.python’]}, ${courses[‘course.algo’]}” in HTML using <p> tag