EL函数能完成对数据的统一操做,其开发步骤以下:java
EL函数只能调用静态方法web
public class MyFunctions { /** * 得到当前日期时间 * @return */ public static String getNowDateTime(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 return sdf.format(new Date()); } }
在WEB-INF目录下创建一个扩展名为tld的xml文件markdown
<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>
若是tld文件是在WEB-INF目录下,就不须要这一步了app
<?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>
经过taglib指令引入外部的函数库jsp
<%@ taglib uri="http://imentors.net.cn/jsp/function" prefix="myfn"%> ${myfn:getNowDateTime() }