In this post, you will learn how to use the <fmt:message> tag in the JSTL format tags library with code example.

The <fmt:message> is used to display the localized messages by replacing the key specified, with the actual message loaded from the resource bundle. This tag works in conjunction with <fmt:bundle> tag which is used to load the resource bundle.

 

JSTL <fmt:message> Syntax:

<fmt:message

   key="<string>"

   bundle="<string>"

   var="<string>"

   scope="<string>"/>

 

Attributes:

Name

Required

Type

Description

    

key

False

java.lang.String

Key of the message to be looked up from the resource bundle.

bundle

False

java.lang.String

Which resource bundle the key to be looked up.

var

False

java.lang.String

Name of the variable to store the localized message.

scope

False

java.lang.String

The scope in which the localized message variable to be stored.

 

JSTL <fmt:message> Example:

The below example displays localized messages from resource bundle net.codejava.jstl.messages. There are two keys we’re looking up in the above said resource bundle namely codejava.welcome.text and codejava.description.text. Both are looked up and actual localized messages are displayed. 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/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>&lt;fmt:message&gt; Demo</title>
  </head>
  <body>
    <h1>&lt;fmt:message&gt; Demo</h1>
    <fmt:bundle basename="net.codejava.jstl.messages">
         The messages displayed using &lt;fmt:message&gt; tags:<br/><br/>
         <fmt:message key="codejava.welcome.text"/><br/>
         <fmt:message key="codejava.description.text"/>
 </fmt:bundle> </body> </html>

 

Output:

message-demo

Recommended Usage of JSTL <fmt:message> tag:

This tag is pretty much used to map the keys with the localized messages specified in the resource bundle or properties file. Optionally we can store the localized message in a scoped variable for later usage instead of retrieving it every time.

 

Other JSTL Format Tags:

bundle  |  formatDate  |  formatNumber  |  param  |  parseDate  |  parseNumber  |  requestEncoding  |  setBundle  |  setLocale  |  setTimeZone  |  timeZone


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#2Ioana Tand2021-02-01 09:29
Buna

ce mai faci?is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.
Quote
#1joe2014-06-24 16:30
would be nice if you showed an example of using the var option.
Quote