Formatting
JSTL provides a set of tags to assist with Internationalization (I18N) of your JSP documents.
-
These tags are also referred to as formatting tags.
-
To use these tags, you must add a different taglib directive to specify the tag library uri and prefix.
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
To format a number, use the <fmt:formatNumber> tag.
-
Use the value attribute to specify the number to format.
- The value can contain JSP EL.
-
Use the type attribute to specify how to format.
- Possible values are
currency,percent, andnumber.
<fmt:formatNumber value="18.99" type="currency"/> <fmt:formatNumber type="currency" value="${cartItem.price }"/> - Possible values are
To format a date, use the <fmt:formatDate> tag.
- For the type attribute specify either
date,time, orboth.
Drill¶
TagLibraries/src/main/webapp/WEB-INF/listStocks.jsp
- Format the Stock price as currency. (Don't forget to add the
fmttaglib directive.)
TagLibraries/src/main/webapp/WEB-INF/select.jsp
- Format the Stock price as currency.
(Solution: TagLibraries/src/main/webapp/WEB-INF/solutions/listStocksFmt.jsp, TagLibraries/src/main/webapp/WEB-INF/solutions/selectFmt.jsp)