幾個階段:
前doStartTag()
中 doAfterBody()
後doEndTag()
優點:有各種處理階段對應的方法,可以在這些階段的方法中,增加自定義方法。
方式2:函數式(一個標籤對應一個方法)
1,新建處理方法
a) 這個方法所在類可以是任意pojo類
b) 這個方法是public static 返回類型 方法名稱(任意參數)
c) 有返回的直接返回,不用考慮放入上下文
2,新增描述
在tld文件中增加類似以下描述即可
<function>
<name>hkName</name>
<function-class>test.org.nutz.taglib.TestFounction</function-class>
<function-signature>java.lang.String getName(java.lang.String)</function-signature>
</function>
3,頁面使用
EL表達式格式
${imsp:**** }
注意:這裏遇到一個問題,上面的代碼複製過會,總是包The function xxx cannot be located with the specified prefix
最後的解決是因爲將原有的function代碼複製就好了,所以估計是空格導致編譯後的function格式不能解析。
1、The function xxx must be used with a prefix when a default namespace is not specified
--- 在jsp頁面中調用方式不正確,可能將 ":" 寫成了 "."
2、The function xxx cannot be located with the specified prefix
--- a) 類中定義的方法不是 public static 的方法
b) 類中的方法名稱和jsp自帶的標籤元素衝突,重名等
優點:簡單靈活。但沒有狀態,在jsp頁面執行到時觸發調用,完成後回到jsp
修改tld文件,是可以不重啓直接使用的 。