How to use resource bundle in JSP?

JSPJava 8Object Oriented ProgrammingProgramming

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 −

AttributeDescriptionRequiredDefault
basenameSpecifies the base name of the resource bundle that is to be loaded.YesNone
PrefixValue to prepend to each key name in <fmt:message> subtagsNoNone

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
raja
Published on 22-Mar-2019 10:47:25
Advertisements