對於一些常用的函式,可以將之撰寫為一個函式庫,之後結合EL中對函式使用的支援即可重複使用該函式,例如您可以這樣使用EL函式:
${ math:gcd(10, 20) }
要能夠自訂EL函式並使用之,我們必須完成四個步驟:
- 撰寫函式類別
- 撰寫標籤函式描述(Tag Library Descriptor)
- 在web.xml中說明class與tld的位置資訊
- 在JSP網頁中指定標籤函式位置與前置文字
一個一個來完成,首先編寫下面的程式:
package onlyfun.caterpillar; public class MathTools { public static int gcd(int m, int n) { int r = 0; while(n != 0) { r = m % n; m = n; n = r; } return m; } public static double pi() { return Math.PI; } }
注意所有的函式都是公開且靜態的,編譯完成之後,將之放置在WEB-INF\classes\下即可,然後撰寫標籤函式描述(Tag Library
Descriptor),這是個XML格式的檔案,注意副檔名要是.tld而不是.xml,假設撰寫的檔名是mathtools.tld:
<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd" version="2.0"> <description>Math Tools</description> <tlib-version>1.0</tlib-version> <short-name>SimpleMathTools</short-name> <uri>/SimpleMathTools</uri>
<function> <description>GCD Tool</description> <name>gcd</name> <function-class> onlyfun.caterpillar.MathTools </function-class> <function-signature> int gcd(int,int) </function-signature> </function> <function> <description>PI Tool</description> <name>pi</name> <function-class> onlyfun.caterpillar.MathTools </function-class> <function-signature> double pi() </function-signature> </function> </taglib>
大部分的標籤光看標籤名就可知道它的作用了(這是XML文件的自描述特性),注意一下<function-
signature>,它與<name>對應,<name>是EL呼叫函式時所用的名稱,而<function-
signature>定義了函式的傳入參數與傳回值。
接下來在web.xml中添加對.tld與類別檔的相關描述:
... <jsp-config> <taglib> <taglib-uri> http://caterpillar.onlyfun.net/ </taglib-uri> <taglib-location> /WEB-INF/tlds/mathtools.tld </taglib-location> </taglib> </jsp-config> ...
<taglib-uri>用來設定使用.tld時的名稱空間識別,這個資訊在JSP網頁中是用來指定將使用哪一個位置的tld檔,接下來直接看看JSP網頁中如何使用定義好的EL函式:
<%@taglib prefix="math" uri="http://www.caterpillar.onlyfun.net/"%> <html> <body> Math Tools GCD Test: ${ math:gcd(100, 14) }<br> Math Tools PI Test: ${ math:pi() } </body> </html>
您使用指令元素taglib來指定tld檔的URI位置,並設定使用時的前置文字,前置文字的作用是當有許多同名函式時(例如用了兩個位置的函式庫,而當中有相同的函式時),可以根據前置文字來識別使用的是哪一個函式。
接下來就是啟動Tomcat並執行了,傳回的結果是:
<html>
<body>
Math Tools GCD Test: 2<br>
Math Tools PI Test: 3.141592653589793
</body>
</html>
附帶一提的是,並不一定要在web.xml中添加對.tld與類別檔的相關描述,如果沒有這個步驟的話,在JSP網頁中直接指定.tld的實體位置也是可以的:
<%@taglib prefix="math" uri="/WEB-INF/tlds/mathtools.tld"%>
在web.xml中定義.tld的資訊是為了管理的方便,如果不定義,則每次更動.tld檔案的位置或名稱,則必須修改每一個JSP網頁,如果有在 web.xml檔中定義,則更動.tld檔案的位置或名稱後,只要修改web.xml中的定義即可。 |