JSTL Format Tag fmt:param Example
- Details
- Written by Nam Ha Minh
- Last Updated on 31 August 2019   |   Print Email
The <fmt:param> tag is used provide parameterized values to the text messages. We use the <fmt:message> tag to display the localized messages and sometimes we needed to parameterize certain values like greeting the user with user name. In this case, we can use <fmt:param>to supply the user name or any other necessary information to the message being displayed.
JSTL <fmt:param> Syntax:
<fmt:param value="<string>"/>
Attributes:
Name | Required | Type | Description |
value | False | java.lang.String | Argument for the parametric replacement. |
JSTL <fmt:param> Example:
The following example displays the greeting message to the user using the <fmt:message> tag and takes the user name from the <fmt:param> tag.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ 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><fmt:param> Demo</title> </head> <body> <h1><fmt:param> Demo</h1> <c:set var="userName" value="Guest"/> <fmt:bundle basename="net.codejava.jstl.messages"> <fmt:message key="codejava.user.greeting"> <fmt:param value="${userName}"/> </fmt:message> <br/> </fmt:bundle> </body> </html>
The above example gets the user name from variable username and passes it using the <fmt:param> tag. However, in real scenario the <fmt:param> tag can get the parameter value from the request scope or any other scope.
When specifying the key value in the resource bundle we’ll have to provide the placeholder to the parameterized value. The following snippet shows how to specify the message key in the resource bundle.
codejava.user.greeting=Hello {0}, and welcome to CodeJava.net
Notice the place holder is specified with {0}which states that the parameter value supplied using the <fmt:param> is used to substitute in this place at the index 0.
Output:
Recommended Usage of JSTL <fmt:param> tag:
The <fmt:param> is used in scenarios where the localized messages are to be personalized or the value to be displayed is dynamic. We can have many parameter placeholders in the resource bundle starting with index 0, we can supply the arguments using <fmt:param> tag.
Other JSTL Format Tags:
bundle | formatDate | formatNumber | message | parseDate | parseNumber | requestEncoding | setBundle | setLocale | setTimeZone | timeZone
Comments
I need Session object in CustomResourceBundle class which extends ListResourceBundle. Pls help me to het that object in customResourceBundle class.