In this tutorial you will learn about the JSTL fmt setBundle tag in JSP.
JSTL fmt Tag setBundle Example
In this tutorial you will learn about the JSTL fmt setBundle tag in JSP.
<fmt:setBundle> tag in JSTL is used in a JSP page to set the resource bundle. A resource bundle is loaded by this tag and is stored into the named scoped variable or the bundle configuration variable.
Attributes of <fmt:setBundle>
- basename : This is a required attribute used for specifying the base name of resource bundle . Basename is a resource name, follows the same component separator dot (.) as the package uses for specifying the fully qualified class name. It does not uses the any type of suffixes such as .properties, .class etc.
- var : This is an optional attribute used for specifying the name of the exported base name to a variable.
- scope : This is an optional attribute is used for specifying the scope of the variable.
Example :
Here an example is being given below will demonstrate you about how to use the JSTL fmt <fmt:setBundle> tag in JSP page. In this example I have created a Properties file named Pop_en_US.properties that contains the element in the manner of key-value mapping and a ListResourceBundle subclasses named Example_en_GB.java and the Location of each class is different i.e one is created in myProperties package and the other one is created in the pack package (however it is not mandatory and you can create the same type of file also) but in both of the classes different values are mapped with the same key. Then created a JSP page into which I gave the complete location of both of the file that has to be loaded using the attribute 'basename' of <fmt:setBundle> tag and specified a name of these exported base name using the var attribute of this tag. So as an output two values will be displayed.
Pop_en_US.properties
HOCKEY=Ice Hockey
Example_en_GB
package pack; import java.util.ListResourceBundle; public class Example_en_GB extends ListResourceBundle { public Object[][] getContents() { return englishLanguage; } static final Object[][] englishLanguage = { {"HOCKEY", "Field Hockey"}, }; }
JstlFmtSetBundle.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ 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:setBundle Tag</title> </head> <body> <fmt:setLocale value="en_GB"/> <fmt:setBundle basename="pack.Example" var="GB"/> Hockey is called '<fmt:message key="HOCKEY" bundle="${GB}"/>' in British English<br> <fmt:setLocale value="en_US"/> <fmt:setBundle basename="myProperties.Prop.Pop" var="US"/> Hockey is called '<fmt:message key="HOCKEY" bundle="${US}" />' in American English </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 JstlFmtSetBundle.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 JstlFmtSetBundle.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 your eclipse browser as :
[ 0 ] Comments