JSTL-fmt标签库setLocale应用实例



JSTL-fmt标签库setLocale应用实例。<fmt:setLocale>标签:用于设置本地化环境,<fmt:formatDate>标签:用于格式化日期设置时区。formatting标签库:就是用于在 JSP 页面中做国际化格式化的动作.
分为了两类,分别是:
国际化核心标签:<fmt:setLocale>、<fmt:bundle>、<fmt:setBundle>、<fmt:message>、<fmt:param>、<fmt:requestEncoding>
格式化标签:<fmt:timeZone>、<fmt:setTimeZone>、<fmt:formatNumber>、<fmt:parseNumber>、<fmt:formatDate>、<fmt:parseDate>

<fmt:setLocale>标签:用于设置本地化环境
属性描述
value:Locale 环境的指定,可以是 java.util.Locale 或 String 类型的实例
scope:Locale 环境变量的作用范围(可选)
如:
设置本地环境为繁体中文
<fmt:setLocale value=”zh_TW”/>
设置本地环境为简体中文
<fmt:setLocale value=”zh_CN”/>

fmt_setLocale.jsp源码

<%@ page language=”java” pageEncoding=”utf-8″%>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/fmt” prefix=”fmt”%>
<%@page import=”java.util.Locale”%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>My JSP ‘fmt_setLocale.jsp’ starting page</title>
</head>
<body>
<%
request.setAttribute(“localeList”, Locale.getAvailableLocales());
%>
<table border=”1″>
<tr class=”title”>
<th>Locale</th>
<th>Language</th>
<th>Date and Time</th>
<th>Number</th>
<th>currency</th>
</tr>
<jsp:useBean id=”date” class=”java.util.Date”></jsp:useBean>
<c:forEach var=”locale” items=”${ localeList }”>
<fmt:setLocale value=”${ locale }” />
<tr>
<td align=”left”>${ locale.displayName }</td>
<td align=”left”>${ locale.displayLanguage }</td>
<td><fmt:formatDate value=”${ date }” type=”both”/></td>
<td><fmt:formatNumber value=”10000.5″ /></td>
<td><fmt:formatNumber value=”10000.5″ type=”currency” /> </td>
</tr>
</c:forEach>
</table>
</body>
</html>

<fmt:formatDate>标签:用于格式化日期
属性描述
value:格式化的日期,该属性的内容应该是 java.util.Date 类型的实例
type:格式化的类型
pattern:格式化模式
var:结果保存变量
scope:变量的作用范围
timeZone:指定格式化日期的时区

fmt_timeZone.jsp源码:


<%@ page language=”java” import=”java.util.*” pageEncoding=”utf-8″%>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/fmt” prefix=”fmt”%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>My JSP ‘fmt_timeZone.jsp’ starting page</title>
</head>
<body >
<%
Map<String, TimeZone> hashMap = new HashMap<String, TimeZone>(); // 时区map

for(String ID : TimeZone.getAvailableIDs()){ // 获取所有JDK中的时区ID
hashMap.put(ID, TimeZone.getTimeZone(ID)); // 保存所有的时区
}
request.setAttribute(“timeZoneIds”, TimeZone.getAvailableIDs()); // 存储到request
request.setAttribute(“timeZone”, hashMap); // 存储到request
%>

<jsp:useBean id=”date” class=”java.util.Date”></jsp:useBean>

<fmt:setLocale value=”zh_CN” />

现在时刻:<%= TimeZone.getDefault().getDisplayName() %>
<fmt:formatDate value=”${ date }” type=”both” /> <br/>
<table border=”1″>
<tr>
<th>时区ID</th>
<th>时区</th>
<th>现在时间</th>
<th>时差</th>
</tr>
<c:forEach var=”ID” items=”${ timeZoneIds }” varStatus=”status”>
<tr>
<td>${ ID }</td>
<td>${ timeZone[ID].displayName }</td>
<td>
<fmt:timeZone value=”${ ID }” >
<fmt:formatDate value=”${ date }” type=”both” timeZone=”${ ID }”/>
</fmt:timeZone>
</td>
<td>${ timeZone[ID].rawOffset / 60 / 60 / 1000 }</td>
</tr>
</c:forEach>
</table>
</body>
</html>