• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

JSTL Format fmt:setLocale Tag

February 22, 2014 //  by Krishna Srinivasan//  Leave a Comment

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

[code lang=”html”] <fmt: setLocale attributes/>
[/code]

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

[code lang=”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;">[/code]

The another ResourceBundle which contains boys name:

Listing 2: SetLocaleForBoys.java

[code lang=”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" }, };
}
[/code]

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

[code lang=”html”] <%@ 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>
[/code]

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 :
JSTL_fmt setLocale output_demo

 

Previous Tutorial : JSTL Format fmt:parseDate Tag :: Next Tutorial : JSTL Format fmt:setBundle Tag

Category: Java EETag: JSTL Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « JSTL Format fmt:parseDate Tag
Next Post: JSTL Format fmt:setBundle Tag »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact