自定義EL函數的使用

自定義EL函數的使用

 

1.在WebRoot\WEB-INF目錄下面建立一個mytag.tld文件。

2.Mytag.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  http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"

    version="2.0">

    <tlib-version>1.0</tlib-version>

    <short-name>el</short-name>

<function>

<!-- 對這個EL方法的描述  -->

       <description>calculate string length</description>

       <name>FunctionsEl</name><!-- 調用EL方法的名稱 -->

        <function-class>com.el.code.FunctionsEl</function-class>

        <function-signature>

             java.lang.String elEncode(java.lang.String)

        </function-signature>

         <example>${el:FunctionsEl(str)}</example><!-- 例如 -->

      </function>

   </taglib>

 

3.在com.el.code包下面新建一個類名爲FunctionsEl的類。

package com.gouwu.youboy.util;

 

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

 

/**

 * @project XXX

 * @author : XXX

 * @version 1.0

 * @Create:2011 5:41:27 PM

 * @Update:

 * @describe:

 */

public class FunctionsEl {

    /**

     * 對中文進行轉碼

     * @param str 中文字符串

     * @return 中文編碼過後的的字符串

     */

    public static String elEncode(String str) {

        String decodeStr = null;

        try {

            decodeStr = URLEncoder.encode(str, "UTF-8");

        } catch (UnsupportedEncodingException e) {

            e.printStackTrace();

        }

        return decodeStr;

    }

 

}

4.JSP頁面引用如下:

<%@ taglib prefix="el" uri="/WEB-INF/mytag.tld"%>

${el:FunctionsEl(iklist)}

 

 

發佈了5 篇原創文章 · 獲贊 5 · 訪問量 25萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章