JSTL Format fmt:setBundle Tag

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

AttributesDescription
basenameSpecifies the resource bundle name with the package name.
varSpecifies the variable name to store new ResourceBundle.
scopeThe 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 =
	  {
	  		{&quot;girls.sangeeta&quot;, &quot;sangeeta&quot;},
	  		{&quot;girls.mitali&quot;, &quot;mitali&quot;},
	  		{&quot;girls.nisha&quot;, &quot;nisha&quot;},
	  };
}

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 :
JSTL_fmt setBundle tag_demo

Previous Tutorial : JSTL Format fmt:setLocale Tag :: Next Tutorial : JSTL Format fmt:formatNumber Tag

Comments

comments

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.

Speak Your Mind

*