The <fmt:setLocale> is used to store the given locale in the locale configuration variable of the given scope or the class.
The Syntax Of <fmt:setLocale> Tag
<fmt: setLocale attributes/>
Attributes Of <fmt:setLocale> Tag
Attributes | Description |
---|---|
Value | It is used to specify the locale name in lower case or in upper case. This value name should be as same as of the class name for rendering the page accordingly. |
Variant | Specifies the locale variant |
Scope | The Scope into which the variable has to be set. |
Example
In first ResourceBundle class we have inserted girls name.
Listing 1: SetLocaleForGirls.java
package javabeat.net; import java.util.ListResourceBundle; <span style="font-size: 12px; line-height: 18px;">public class SetLocaleForGirls extends ListResourceBundle {</span> public Object[][] getContents() { return contents; } static final Object[][] contents = { { "girls.naila", "naila" }, { "girls.mitali", "mitali" }, { "girls.rushali", "rushali" }, }; } <span style="font-size: 12px; line-height: 18px;">
The another ResourceBundle which contains boys name:
Listing 2: SetLocaleForBoys.java
package javabeat.net; import java.util.ListResourceBundle; public class SetLocaleForBoys extends ListResourceBundle { public Object[][] getContents() { return contents; } static final Object[][] contents = { { "boys.vikrant", "vikrant" }, { "boys.ganesh", "ganesh" }, { "boys.mahantesh", "mahantesh" }, }; }
Details of the Code
ListResourceBundle is abstract subclass of resource bundle. It is used to manage the list of values. The class SetLocaleForBoys and SetLocaleForGirls extends the ListResourceBundle to get list of values by using getContent method.
Listing 3: 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>SetLocale Tag</title> </head> <body> <fmt:bundle basename="javabeat.net.SetLocaleForGirls" prefix="girls."> <fmt:message key="naila" /> <br /> <fmt:message key="mitali" /> <br /> <fmt:message key="rushali" /> <br /> </fmt:bundle> <fmt:setLocale value="Boys" /> <fmt:bundle basename="javabeat.net.SetLocaleForBoys" prefix="boys."> <fmt:message key="vikrant" /> <br /> <fmt:message key="ganesh" /> <br /> <fmt:message key="mahantesh" /> <br /> </fmt:bundle> </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.
- In basename we have declared the package name (javabeat.net) and resource bundle name (SetLocaleForGirls).
- Prefix girls is used.
- <fmt: setLocale value= “boys”/> set the locale in the jsp file of the second ResourceBundle Class i.e SetLocaleForBoys.
Steps for Execution
- Save this file as example.jsp in your eclipse IDE. Compile the classes SetLocaleForBoys and SetLocaleForGirls.
- 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:parseDate Tag :: Next Tutorial : JSTL Format fmt:setBundle Tag