The <fmt:setBundle> tag is used to creates a ResourceBundle and stores the ResourceBundle object into the given variable and scope.
The Syntax Of <fmt:setBundle> Tag
<fmt: setBundle basename= “ResourceBundle Name” var = “var name”/>
Attributes Of <fmt:setBundle> Tag
Attributes | Description |
---|---|
basename | Specifies the resource bundle name with the package name. |
var | Specifies the variable name to store new ResourceBundle. |
scope | The Scope into which the variable has to be set. |
Example
Listing 1: SetBundleExample.java
package javabeat.net; import java.util.ListResourceBundle; public class SetBundleExample extends ListResourceBundle { public Object[ ][ ] getContents() { return contents; } static final Object[ ][ ] contents = { {"girls.sangeeta", "sangeeta"}, {"girls.mitali", "mitali"}, {"girls.nisha", "nisha"}, }; }
Details of the Code
ListResourceBundle is used to manage the list of values. The class SetBundleExample extends the ListResourceBundle to get list of values by using getContent method.
Listing 1: example.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>setBundle Example</title> </head> <body> <fmt:setLocale value="ex" /> <fmt:setBundle basename="javabeat.net.SetBundleExample" var="lang" /> <fmt:message key="girls.sangeeta" bundle="${lang}" /> <br /> <fmt:message key="girls.mitali" bundle="${lang}" /> <br /> <fmt:message key="girls.nisha" bundle="${lang}" /> <br /> </body> </html>
Details of the Code:
- <%@ taglib uri=”http://java.sun.com/jsp/jstl/fmt” prefix=”fmt” %> tag is used for formatting the data in jsp page.
- <fmt:setLocale value= “ex”/> sets the locale in the jsp file of the ResourceBundle Class.
- In setBundle basename we have declared the package name (javabeat.net) and resource bundle name (SetBundleExample).
- var is used for specifying the variable name.
- bundle is used to get the value of specified key.
Steps for Execution
- Save this file as example.jsp in your eclipse IDE.
- Now select this jsp file, right mouse click and select Run as ->Run on server
Output
When the execution process is completed successfully we will get the following output :
Previous Tutorial : JSTL Format fmt:setLocale Tag :: Next Tutorial : JSTL Format fmt:formatNumber Tag