JSTL - Core <fmt:bundle> Tag


Advertisements

The <fmt:bundle> tag will make the specified bundle available to all <fmt:message> tags that occur between the bounding <fmt:bundle> and </fmt:bundle> tags. With this, you need not specify the resource bundle for each of your <fmt:message> tags.

For example, the following two <fmt:bundle> blocks will produce the same output −

<fmt:bundle basename = "com.tutorialspoint.Example">
   <fmt:message key = "count.one"/>
</fmt:bundle>

<fmt:bundle basename = "com.tutorialspoint.Example" prefix = "count.">
   <fmt:message key = "title"/>
</fmt:bundle>

Attribute

The <fmt:bundle> tag has the following attributes −

Attribute Description Required Default
basename Specifies the base name of the resource bundle that is to be loaded. Yes None
Prefix Value to prepend to each key name in <fmt:message> subtags No None

Example

Resource bundles contain locale-specific objects. Resource bundles contain key/value pairs. When your program needs a locale-specific resource, you keep all the keys common to all the locale but you can have translated values specific to locale. Resource bundles help in providing specific content to locale.

A Java resource bundle file contains a series of key-to-string mappings. The method that we focus on involves creating compiled Java classes that extend the java.util.ListResourceBundle class. You must compile these class files and make them available to the classpath of your Web application.

Let us define a default resource bundle as follows −

package com.tutorialspoint;

import java.util.ListResourceBundle;

public class Example_En extends ListResourceBundle {
   public Object[][] getContents() {
      return contents;
   }
   static final Object[][] contents = {
      {"count.one", "One"},
      {"count.two", "Two"},
      {"count.three", "Three"},
   };
}

Let us compile the above class Example.class and make it available in the CLASSPATH of your Web application. Now you can use the following JSTL tags to display the three numbers as follows −

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/fmt" prefix = "fmt" %>

<html>
   <head>
      <title>JSTL fmt:bundle Tag</title>
   </head>

   <body>
      <fmt:bundle basename = "com.tutorialspoint.Example" prefix = "count.">
         <fmt:message key = "one"/><br/>
         <fmt:message key = "two"/><br/>
         <fmt:message key = "three"/><br/>
      </fmt:bundle>
   </body>
</html>

The above code will generate the following result −

One 
Two 
Three

Try the above example without prefix as follows −

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/fmt" prefix = "fmt" %>

<html>
   <head>
      <title>JSTL fmt:bundle Tag</title>
   </head>

   <body>
      <fmt:bundle basename = "com.tutorialspoint.Example">
         <fmt:message key = "count.one"/><br/>
         <fmt:message key = "count.two"/><br/>
         <fmt:message key = "count.three"/><br/>
      </fmt:bundle>
   </body>
</html>

The above code will generate the following result −

One 
Two 
Three

Check <fmt:setLocale> and <setBundle> tags to understand the complete concept.

jsp_standard_tag_library.htm

Useful Video Courses


Video

JSP, Servlet, JSLT + Hibernate: A complete guide

108 Lectures 11 hours

Chaand Sheikh

Video

Full Stack Java developer - Java + JSP + Restful WS + Spring

Best Seller

517 Lectures 57 hours

Chaand Sheikh

Video

Servlets and JSP Tutorial For Beginners!

41 Lectures 4.5 hours

Karthikeya T

Video

JSP and Servlets - The Complete Course

42 Lectures 5.5 hours

TELCOMA Global

Video

JSP and Servlets Bootcamp: Web Applications for Beginners

15 Lectures 3 hours

TELCOMA Global

Video

JSP (Java Server Pages) Certification Training

44 Lectures 15 hours

Uplatz

Advertisements