Open In App

JSTL Formatting <fmt:setBundle> Tag

Last Updated : 29 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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>

GeeksBundle.java

Java




//Java Program Bundle Object
  
package com.geeksforgeeks;
import java.util.ListResourceBundle;
  
//Class to manage bundle
public class GeeksBundle 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 GeeksBundle.java and also assure it is available in the CLASSPATH of your application.

setBundle.jsp

Ensure both GeeksBundle.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.geeksforgeeks.GeeksBundle" var="courses"/>
      <p>${courses['course.java']}</p>
      <p>${courses['course.python']}</p>
      <p>${courses['course.algo']}</p>
  </body>
</html>


Output:

Output of setBundle.jsp

Output

Explanation of the above Program:

  • In the above example, we have used the resource bundle as “GeeksBundle
  • 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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads