[TOC]
EL函數能完成對資料的統一操作,其開發步驟如下:
- 開發函數處理類,處理類就是普通的類;每個函數對應類中的一個靜态方法;
- 建立TLD檔案,定義表達式函數
- 在WEB.XML檔案中配置(可省略)
- 在JSP頁面内導入并且使用
一、定義一個普通類,提供實作功能的靜态方法
EL函數隻能調用靜态方法
public class MyFunctions {
/**
* 獲得目前日期時間
* @return
*/
public static String getNowDateTime(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設定日期格式
return sdf.format(new Date());
}
}
二、EL函數的配置
<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>myfn</short-name>
<uri>http://imentors.net.cn/jsp/function</uri>
<function>
<description>
獲得目前日期時間
</description>
<!--這裡name可以随便寫-->
<name>getNowDateTime</name>
<!--這裡最為重要,指定類所在位置,以及類方法的一些重要資訊-->
<function-class>cn.net.imentors.javaweb.el.MyFunctions</function-class>
<!--函數的傳回值和參數必須是全類名-->
<function-signature>java.lang.String getNowDateTime()</function-signature>
</function>
</taglib>
三、在WEB中配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<jsp-config>
<taglib>
<!-- 配置标簽的引用位址 JSP頁面中引用時使用-->
<taglib-uri>/myfn</taglib-uri>
<!-- 配置标簽的TLD檔案位址 -->
<taglib-location>/WEB-INF/myfn.tld</taglib-location>
</taglib>
</jsp-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
四、在JPS中使用
<%@ taglib uri="http://imentors.net.cn/jsp/function" prefix="myfn"%>
${myfn:getNowDateTime() }