EL自定义函数(转)

 EL自定义函数实现步骤:
1.开发函数处理类,即普通的Java类;每个函数对应类中的一个静态方法。
2. 建立TLD(Tag Library Descriptor),定义表达式函数。
3.在web.xml中配置TLD文件位置。
4.在JSP页面中使用自定义函数。
因为EL表达式函数,主要功能是完成对数据的修改,统一化格式,所有第一步有时候不需要我们自己来写,使用java.lang.Math中的一些方法也是可能实现的,下面就先介绍这种情况。

一、使用java.lang.Math中的方法
由于是使用java中已有的类,所以第一步就可以不用写了。
1.首先在WEB-INF下面建立一个TLD文件:custom-function.tld

View Code
 1 <!-- 头文件属性必须要 -->
2 <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
5 version="2.0">
6 <!-- 标记库版本信息要有 -->
7 <tlib-version>1.0</tlib-version>
8
9 <!-- 取绝对值 -->
10 <function>
11 <name>abs</name>
12 <function-class>java.lang.Math</function-class>
13 <function-signature>int abs(int)</function-signature>
14 </function>
15
16 <!-- 取近似值 -->
17 <function>
18 <name>round</name>
19 <function-class>java.lang.Math</function-class>
20 <function-signature>int round(double)</function-signature>
21 </function>
22
23 <!-- 取最大值 -->
24 <function>
25 <name>max</name>
26 <function-class>java.lang.Math</function-class>
27 <function-signature>int max(int,int)</function-signature>
28 </function>
29
30 <!-- 求开方值 -->
31 <function>
32 <name>sqrt</name>
33 <function-class>java.lang.Math</function-class>
34 <function-signature>double sqrt(double)</function-signature>
35 </function>
36
37 <!-- 求正弦值:是以弧度计算的 -->
38 <function>
39 <name>sin</name>
40 <function-class>java.lang.Math</function-class>
41 <function-signature>double sin(double)</function-signature>
42 </function>
43 </taglib>

 2.在web.xml中配置TLD文件位置,配置如下:

View Code
 1 <?xml version="1.0" encoding="utf-8"?>
2
3 <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
6 version="2.4">
7
8 <jsp-config>
9 <taglib>
10 <!-- 此处uri可以自己随便定义,但后面用时一定与这里一样 -->
11 <taglib-uri>
12 http://chaozhichen.iteye.com/custom-function-tablib
13 </taglib-uri>
14 <!-- tld文件的路径 -->
15 <taglib-location>
16 /WEB-INF/tld/custom-function.tld
17 </taglib-location>
18 </taglib>
19 </jsp-config>
20
21 <welcome-file-list>
22 <welcome-file>index.jsp</welcome-file>
23 </welcome-file-list>
24 </web-app>

3.在JSP页面使用自定义函数
custom_function.jsp

View Code
 1 <%@ page pageEncoding="utf-8" %>
2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
3 <!-- 这里的uri用户可以随便指定,只要与web.xml配置的taglib-uri一致就可以-->
4 <%@ taglib prefix="czc" uri="http://chaozhichen.iteye.com/custom-function-tablib"%>
5
6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
7 <html>
8 <head>
9 <title>EL Custom Function Examples</title>
10 </head>
11
12 <body>
13 <h1>EL Custom Function Examples</h1>
14 <c:set var="num" value="-100"></c:set>
15 The absolute value of ${num} is ${czc:abs(num)}.<br>
16 <c:set var="cout" value="${1+2/3}"></c:set>
17 The rounded value of ${cout} is ${czc:round(cout)}.<br>
18 The max value of 10 and 8 is ${czc:max(10,8)}.<br>
19 <c:set var="sixteen" value="16"></c:set>
20 The sqrt value of ${sixteen} is ${czc:sqrt(sixteen)}.<br>
21 <c:set var="degree" value="45"></c:set>
22 The sin value of ${degree} is ${czc:sin(degree)}.<br>
23 </body>
24 </html>

测试结果如下:



二、用户自定义类,实现将数字转换成汉字
1.开发自定义处理函数类
ELFunction.java

View Code
 1 public class ELFunction {
2
3 /**
4 * 将数字转换成大写
5 *
6 * @param num:
7 * 要转换的数字
8 * @return
9 */
10 public static String covertNumberToChinese(int num) {
11 // 定义一个汉字数组
12 String[] chinese = { "", "", "", "", "", "", "", "", "", "" };
13 // 将数字转换成字符串
14 String str = "" + num;
15 String relStr = "";
16 for (int i = 0; i < str.length(); i++) {
17 // 得到单个数字
18 int single = Integer.parseInt(str.valueOf(str.charAt(i)));
19 System.out.print(single + " ");
20 // 转换成汉字
21 relStr += chinese[single];
22 }
23 return relStr;
24 }
25 }

 2.建立TLD文件,在前面TLD文件的基础上添加如下代码

View Code
1 <!-- 自定义函数:将数字转换成大写 -->
2 <function>
3 <name>covertNumberToChinese</name>
4 <function-class>cn.netjava.util.ELFunction</function-class>
5 <function-signature>String covertNumberToChinese(int)</function-signature>
6 </function>

3.在web.xml中就不用再配置了,在JSP页面加入如下代码

View Code
1     <!-- 调用自定义函数 -->
2 <c:set var="convert" value="2435"></c:set>
3 <b>自定义函数结果:${czc:covertNumberToChinese(convert)}</b>

 测试结果如下:

原帖:http://chaozhichen.iteye.com/blog/972455

posted @ 2011-09-04 12:05  zhjb616  阅读(451)  评论(0编辑  收藏  举报