JSTL <fmt:setLocale> 标签用于设置用户本地化环境。
JSP <fmt:setLocale> 标签的语法如下:
- <fmt:setLocale value="localcode" [scope="page|request|session|application"] [variant="variant"]>
其中:
<fmt:setLocale> 标签的简单示例如下:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ page import="java.util.Locale"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
- <!DOCTYPE html>
- <html>
- <head>
- <title>城东书院(www.cdsy.xyz)</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>
Locale.getAvailableLocales() 方法返回所有已安装语言环境的数组集合。
注意:由于篇幅有限,此处语言环境以及时间只展示了一部分,大家可自己运行查看。