上接JSP總結(二)
Ⅲ.XML標籤庫 <x:…>
1.XML(Extensible Markup Language)可擴展標記語言,與HTML(Hypertext Markup Language)的區別是:
<1>所有標籤必須關閉,而HTML中如<hr>不關閉;
<2>大小寫敏感;
<3>必須正確嵌套,否則不能解析;
<4>必須有一個根元素;
<5>屬性值必須加引號.
如:book.xml文件
<?xml version="1.0"encoding="UTF-8"?>
<books>
<book isbn="23421">
<title>Java</title>
<author>z3</author>
<price>23</price>
</book>
<book isbn="23432">
<title>Javascript</title>
<author>w5</author>
<price>20</price>
</book>
</books>
2.Xpath:在XML文檔中查找信息的語言。
3.在.jsp上xml文檔,有以下幾種方式:
<1>外部導入:
<!-- 外部導入xml文檔 -->
<c:import url="books.xml"var="xmldoc"></c:import>
<!-- 解析xml -->
<x:parse doc="${xmldoc}"var="doc"></x:parse>
<!-- 返回結果集中的第一個元素的字符串 -->
<x:out select="$doc//book"/> <!-- 注意此處不能加大括號 -->
<2>直接寫在解析標籤內部:
<x:parse var="doc">
<%@ include file="books.xml"%>
</x:parse>
<!-- 返回結果集中的第一個元素的字符串 -->
<x:out select="$doc//book"/> <!--注意此處不能加大括號 -->
Ⅳ.Function標籤庫 <fn:…>
如:
${fn:toLowerCase("USA")}<br>
${fn:substring('I am astudent',2,4)}<br> <!--取從位置2開始到位置4之前的字符串 -->
${fn:substringAfter('Iam a student',"am")}<br> <!-- 取am後的字符串 -->
<%
String[] arr={"a","b","c"};
request.setAttribute("arr", arr);
%>
${fn:join(arr,":")} <br> <!-- 將字符數組內的字符之間用指定的符號連接起來-->
${fn:escapeXml('<a>fd</a>')}<br> <!-- 轉義爲實體 -->
Ⅴ.國際化<fmt:…>
1、國際化(internationalization,簡稱”i18n”):程序員要做的
本地化(localization,簡稱”l10n”):用戶
2、要唯一標示一個文化區域:語言+國家(區域)
如:中國(內地) zh_CN
美國 en_US
英國 en_GB
3、國際化的步驟:
<1>新建相應國家(地區)的屬性文件(.properties),建在src文件夾下,添加屬性名(name)和屬性值(value).
如:
新建msg_zh_CN.properties和msg_en_US.properties文件,
.jsp文件相應代碼如下:
<%
request.setAttribute("now", new Date());
%>
<!-- 設置區域 -->
<fmt:setLocale value="en_US"/>
<!-- 設置基本名:包名+屬性文件的前綴名 -->
<fmt:bundle basename="i18n.msg">
<!—讀取相應的屬性值-à
<fmt:message key="login.name">
</fmt:message><br>
<fmt:message key="login.time">
<fmt:param value="${now}"></fmt:param>
</fmt:message>
</fmt:bundle>
<h1>簡化key的書寫</h1>
<fmt:setLocale value="${fn:substringBefore(header['Accept-Language'],',')}"/>
<fmt:bundle basename="i18n.msg"prefix="login">
<fmt:message key=".name">
</fmt:message><br>
<fmt:message key=".time">
<fmt:param value="${now}"></fmt:param>
</fmt:message>
</fmt:bundle>
<h1>第二種設置資源文件的方法 setBundle</h1>
<fmt:setBundle var="bu" basename="i18n.msg"/>
<fmt:message key="login.name"bundle="${bu}">
</fmt:message><br>
<fmt:message key="login.time"bundle="${bu }">
<fmt:param value="${now}"></fmt:param>
</fmt:message>
4.國際化數字
<!-- 貨幣 -->
<fmt:setLocale value="zh_CN"/>
<fmt:formatNumber value="2000"type="currency"></fmt:formatNumber><br>
<fmt:setLocale value="en_GB"/>
<fmt:formatNumber value="2000"type="currency"></fmt:formatNumber><br>
<fmt:setLocale value="en_US"/>
<fmt:formatNumber value="2000"type="currency"></fmt:formatNumber><br>
</body>
<!-- 數字 -->
<fmt:setLocale value="zh_CN"/>
<fmt:formatNumber value="2000"type="number"></fmt:formatNumber><br>
<fmt:setLocale value="en_GB"/>
<fmt:formatNumber value="2000"type="number"></fmt:formatNumber><br>
<fmt:setLocale value="en_US"/>
<fmt:formatNumber value="2000"type="number"></fmt:formatNumber><br>
<!-- 百分比 -->
<fmt:setLocale value="zh_CN"/>
<fmt:formatNumber value="2000"type="percent"></fmt:formatNumber><br>
<fmt:setLocale value="en_GB"/>
<fmt:formatNumber value="2000"type="percent"></fmt:formatNumber><br>
<fmt:setLocale value="en_US"/>
<fmt:formatNumber value="2000" type="percent"></fmt:formatNumber><br>
5.國際化日期
<%
request.setAttribute("now", new Date());
%>
<fmt:setLocale value="zh_CN"/>
<fmt:formatDate value="${now}"type="both" pattern="yyyy年mm月dd日 hh:mm:ss"/><br>
<fmt:setLocale value="en_GB"/>
<fmt:formatDate value="${now}"type="both" /><br>
<fmt:setLocale value="en_US"/>
<fmt:formatDate value="${now}"type="both" /><br>