第一:写个类:
package com.el.function;
public class PrintFormatTime {
public static String printTime(String pattern){
if(pattern.equals("yyyy-MM-dd")){
return "2008-5-25";
}
return "error formater";
}
}
第二:写个elfunction.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>by Brj</description>
<display-name>EL Function</display-name>
<tlib-version>1.1</tlib-version>
<short-name>format</short-name>
<uri>http://www.mingloveyang.com</uri>
<function>
<name>format</name>
<function-class>com.el.function.PrintFormatTime</function-class>
<function-signature>java.lang.String printTime(java.lang.String)</function-signature>
</function>
</taglib>
第三:web.xml这样中定义
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<jsp-config>
<taglib>
<taglib-uri>http://www.yang.com</taglib-uri>
<taglib-location>/WEB-INF/elfunction.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
最后:页面中就可以这样用了
${fmt:format("yyyy-MM-dd") }
便会打印 '2008-5-25'
其中需要这样声明在web.xml文件中定义的uri:
<%@ taglib prefix="fmt" uri="http://www.yang.com" %>