`

自訂EL函式

    博客分类:
  • jstl
阅读更多
資料來源: Java 技術論壇
作者: 良葛格(caterpillar)

對於一些常用的函式,我們可以將之撰寫為一個函式庫,之後結合EL中對函式使用的支援即可重複使用該函式,例如我們可以這樣使用EL函式:

${ math:gcd(10, 20) }

要能夠自訂EL函式並使用之,我們必須完成四個步驟:

  1. 撰寫函式類別。
  2. 撰寫標籤函式描述(Tag Library Descriptor)。
  3. 在web.xml中說明class與tld的位置資訊。
  4. 在JSP網頁中指定標籤函式位置與前置文字。

 

1.撰寫函式類別

我們一個一個來完成,首先我們編寫下面的程式:

package demo.el;

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;
    }
}

2.撰寫標籤函式描述(Tag Library Descriptor)

注意所有的函式都是公開且靜態的,編譯完成之後,將之放置在WEB-INF\classes\下即可,然後我們撰寫標籤函式描述(Tag Library Descriptor),這是個XML格式的檔案,注意副檔名要是.tld而不是.xml,假設我們的檔名是mathtools.tld:

 

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>
<!-- 注意2个问题 1 name 要和 方法名一致 2 方法参数和返回类型的字符串要和 代码返回一致 (这里是不支持泛型的)
 -->    <function>
        <description>GCD Tool</description>
        <name>gcd</name>
        <function-class>demo.el.MathTools</function-class>
        <function-signature>int gcd(int,int)</function-signature>    
    </function>
    <function>
        <description>PI Tool</description>
        <name>pi</name>
        <function-class>demo.el.MathTools</function-class>
        <function-signature>double pi()</function-signature>    
    </function>

</taglib>

大部分的標籤光看標籤名就可知道它的作用了(這是XML文件的自描述特性),我們注意一下<function-signature>,它與<name>對應,<name>是EL呼叫函式時所用的名稱,而<function-signature>定義了函式的傳入參數與傳回值。

3.在web.xml中說明class與tld的位置資訊

接下來我們在web.xml中添加對.tld與類別檔的相關描述:

web.xml
<jsp-config>
    <taglib>
        <taglib-uri>http://www.caterpillar.onlyfun.net/phpBB2</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/phpBB2"%>
<html>
<body>
    Math Tools GCD Test: ${ math:gcd(100, 14) }<br>
    Math Tools PI Test: ${ math:pi() }
</body>
</html>

4.在JSP網頁中指定標籤函式位置與前置文字

我們使用指令元素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中的定義即可,當中維護在方便性的差別上可見一般。

<!-- <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=1046" dc:identifier="http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=1046" dc:title="自訂EL函式" trackback:ping="http://www.javaworld.com.tw/confluence/rpc/trackback/1046" /> </rdf:RDF> --><!-- Root decorator: all decisions about how a page is to be decorated via the inline decoration begins here. --><!-- Switch based upon the context. However, for now, just delegate to a decorator identified directly by the context. -->

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics