- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use resource bundle in JSP?
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
- Related Articles
- How to use multiple resource bundle in same JSP?
- How to set locate to identify required resource bundle in JSP?
- How to use action in JSP?
- How to use action in JSP?
- How to use action in JSP?
- How to use Action in JSP?
- How to use Action in JSP?
- How to use a filter in JSP?
- How to use time zone in a JSP?
- How to use parameterized SQL query in a JSP?
- How to get Resource name using Resource id in Android?
- How to use the tag to define a relationship to an external resource?
- How to use Boto3 to download an object from S3 using AWS Resource?
- I want to use %> literal in JSP page. But it is throwing error. How to escape this syntax in JSP?
- How to use Boto3 library in Python to upload an object in S3 using AWS Resource?
