<span style="font-size:18px;"><?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<jsp-config>
<taglib>
<!-- 此处uri可以自己随便定义,但后面用时一定与这里一样 -->
<taglib-uri>
http://chaozhichen.iteye.com/custom-function-tablib
</taglib-uri>
<!-- tld文件的路径 -->
<taglib-location>
/WEB-INF/tld/custom-function.tld
</taglib-location>
</taglib>
</jsp-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
</span>
EL自定义函数实现步骤:
1.开发函数处理类,即普通的Java类;每个函数对应类中的一个静态方法。(方法都是static修饰)
2. 建立TLD(Tag Library Descriptor),定义表达式函数。 (在WEB-INF下定义)
3.在web.xml中配置TLD文件位置。 (类似初始化)
4.在JSP页面中使用自定义函数。 (页面调用)
一、java中自带方法实现
1、java中的类已经存在,该部略去
2、在WEB-INF下面建立一个TLD文件:custom-function.tld (这个TLD的名字自己定义,不要太离谱)
<span style="font-size:18px;"><!-- 头文件属性必须要 -->
<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">
<!-- 标记库版本信息要有 -->
<tlib-version>1.0</tlib-version>
<!-- !模板中的标准写法 -->
<function>
<name>方法名称</name>
<function-class>java类所在包</function-class>
<function-signature>定义的方法</function-signature>
</function>
<!-- 取绝对值 -->
<function>
<name>abs</name>
<function-class>java.lang.Math</function-class>
<function-signature>int abs(int)</function-signature>
</function>
<!-- 取近似值 -->
<function>
<name>round</name>
<function-class>java.lang.Math</function-class>
<function-signature>int round(double)</function-signature>
</function>
<!-- 取最大值 -->
<function>
<name>max</name>
<function-class>java.lang.Math</function-class>
<function-signature>int max(int,int)</function-signature>
</function>
<!-- 求开方值 -->
<function>
<name>sqrt</name>
<function-class>java.lang.Math</function-class>
<function-signature>double sqrt(double)</function-signature>
</function>
<!-- 求正弦值:是以弧度计算的 -->
<function>
<name>sin</name>
<function-class>java.lang.Math</function-class>
<function-signature>double sin(double)</function-signature>
</function>
</taglib> </span>
3、web.xml中配置TLD文件位置,配置如下:
4、在JSP页面使用自定义函数 custom_function.jsp
<span style="font-size:18px;"><%@ page pageEncoding="utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!-- 这里的uri用户可以随便指定,只要与web.xml配置的taglib-uri一致就可以-->
<%@ taglib prefix="czc" uri="http://chaozhichen.iteye.com/custom-function-tablib"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>EL Custom Function Examples</title>
</head>
<body>
<h1>EL Custom Function Examples</h1>
<c:set var="num" value="-100"></c:set>
The absolute value of ${num} is ${czc:abs(num)}.<br>
<c:set var="cout" value="${1+2/3}"></c:set>
The rounded value of ${cout} is ${czc:round(cout)}.<br>
The max value of 10 and 8 is ${czc:max(10,8)}.<br>
<c:set var="sixteen" value="16"></c:set>
The sqrt value of ${sixteen} is ${czc:sqrt(sixteen)}.<br>
<c:set var="degree" value="45"></c:set>
The sin value of ${degree} is ${czc:sin(degree)}.<br>
</body>
</html>
</span>
二、EL自定义函数
1、自定义方法
<span style="font-size:18px;">public class ViewTool {
/**
* 时间戳格式化
* @param seconds
* @param formatStr
* @return
*/
public static String formatDateBySeconds(String seconds,String formatStr) {
if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
return "";
}
if(formatStr == null || formatStr.isEmpty()) formatStr = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat dateFormatter = new SimpleDateFormat(formatStr);
return dateFormatter.format(new Date(Long.valueOf(seconds+"000")));
}
}</span>
2、在WEB-INF下自定tld,定义ifunction.tld文件
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" 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">
<tlib-version>1.0</tlib-version>
<short-name>function</short-name>
<!-- 定义该标签库的URI -->
<uri>/tags/ifunction</uri>
<function>
<name>formatDateBySeconds</name>
<function-class>com.inspur.iop.om.util.ViewTool</function-class>
<function-signature>java.lang.String formatDateBySeconds( java.lang.String, java.lang.String )</function-signature>
</function>
</taglib>
</span>
3、在web.xml中声明这个tld文件
<span style="font-size:18px;"><?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<jsp-config>
<taglib>
//taglib-uri中的路径自己定义,但是在jsp页面中使用时,那个url必须和这个一致
<taglib-uri>/tags/ifunction</taglib-uri>
<taglib-location>/WEB-INF/classes/ifunction.tld</taglib-location>
</taglib>
</jsp-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
</span>
4、jsp页面中使用
<span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" buffer="none"%>
<%@ taglib uri="/tags/ifunction" prefix="ifn"%>
系统当前时间:
${ifn:formatDateBySeconds(item.operate_time, "yyyy年MM月dd日 HH:mm:ss")} </span>