JSTL fmt Tag setLocale Example

JSTL fmt Tag setLocale Example


Posted in : Java Posted on : April 4, 2012 at 7:50 PM Comments : [ 0 ]

In this tutorial you will learn about how to use the JSTL fmt tag setLocale in JSP.

JSTL fmt Tag setLocale Example

In this tutorial you will learn about how to use the JSTL fmt tag setLocale in JSP.

setLocale tag sets the specified locale provided in its attribute 'value'. This tag keeps the specified locale in the configuration variable of locale.

Attributes of <fmt:setLocale>

  • value : This is a required attribute and used for specifying a locale. To specify a locale code it must have made from the two-letter (lower case) language code (according to ISO-639) and/or the two-letter (upper-case) of country code (according to the ISO-3166). And if both the language and country code are used for specifying the locale the locale code must be separated by either hyphen (-) or underscore (_).
  • variant : This attribute is used for specifying the browser or vendor specific variant.
  • scope : This attribute specifies the scope of locale configuration variable.

Example :

Here a simple example is given for you that will demonstrate how can the <fmt:setLocale> tag be used in a JSP page. In this example at first I have created two ListResourceBundle subclasses. Both classes contains the key-value pair. In Example_en_US.java for the specified key the value is in English language and in Example_fr_FR.java for the same key as of the Example_en_US.java, the value is in French language. Then created a JSP page named JstlFmtSetLocale.jsp into which I have set the locale two times for the sake of understanding you about the <fmt:setLocale> as well as I have used the <fmt:message> tag which maps the key from the key-value localized message. As an output it will display the both locale that you have created in the java classes.

Example_en_US.java

package pack;

import java.util.ListResourceBundle;

public class Example_en_US extends ListResourceBundle {
public Object[][] getContents() {
return englishLanguage;
}
static final Object[][] englishLanguage = {
{"Name", "a"},
{"Address", "i"},
{"Number", "996677554"},
};
}

Example_fr_FR.java

package pack;

import java.util.ListResourceBundle;

public class Example_fr_FR extends ListResourceBundle {
public Object[][] getContents() {
return frenchLanguage;
}
static final Object[][] frenchLanguage = {
{"Name", "un"},
{"Address", "je"},
{"Number", "996677554"},
};
}

JstlFmtSetLocale.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL fmt:setLocale Tag</title>
</head>
<body>
<b>English Locale</b><br>
<fmt:setLocale value="en_US"/>
<fmt:bundle basename="pack.Example">
<fmt:message key="Name"/><br/>
<fmt:message key="Address"/><br/>
<fmt:message key="Number"/><br/>
</fmt:bundle>
<b>French Locale</b><br>
<!-- Change the Locale -->
<fmt:setLocale value="fr_FR"/>
<fmt:bundle basename="pack.Example">
<fmt:message key="Name"/><br/>
<fmt:message key="Address"/><br/>
<fmt:message key="Number"/><br/>
</fmt:bundle>

</body>
</html>

How to run this example

Here I am using an IDE Eclipse so I am giving the process of executing this example in perspective of Eclipse. Before executing this example you will have needed to add the following jar files :

  • jstl.jar
  • standard.jar

After adding of these jar files you may execute your program in the following ways :

  • Select JstlFmtSetLocale.jsp file of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • On the Eclipse Editor go to your JstlFmtSetLocale.jsp -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • Go to Run button look at the toolbar in green color and click -> Choose your server -> Finish.
  • A simplest way to execute the example in Eclipse is to use the CTRL+F11 key -> Run On Server -> Choose your server -> Finish

NOTE : In all of the above execution processes you may start the server first and stop the server each time after the execution if not, each time you will may prompted to a dialog box to Restart the server in Eclipse.

Output :

When the execution process will be completed successfully an output will be displayed on the eclipse browser as :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics