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

Xml代码 复制代码  收藏代码
  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>  
<!-- 头文件属性必须要 -->
<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>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>



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

Xml代码 复制代码  收藏代码
  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>  
<?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>


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

Jsp代码 复制代码  收藏代码
  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>  
<%@ 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>


测试结果如下:



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

Java代码 复制代码  收藏代码
  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. }  
public class ELFunction {

	/**
	 * 将数字转换成大写
	 * 
	 * @param num:
	 *            要转换的数字
	 * @return
	 */
	public static String covertNumberToChinese(int num) {
		// 定义一个汉字数组
		String[] chinese = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
		// 将数字转换成字符串
		String str = "" + num;
		String relStr = "";
		for (int i = 0; i < str.length(); i++) {
			// 得到单个数字
			int single = Integer.parseInt(str.valueOf(str.charAt(i)));
			System.out.print(single + "  ");
			// 转换成汉字
			relStr += chinese[single];
		}
		return relStr;
	}
}



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

Xml代码 复制代码  收藏代码
  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>  
<!-- 自定义函数:将数字转换成大写 -->
	<function>
		<name>covertNumberToChinese</name>
		<function-class>cn.netjava.util.ELFunction</function-class>
		<function-signature>String covertNumberToChinese(int)</function-signature>
	</function>



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

Jsp代码 复制代码  收藏代码
  1. <!-- 调用自定义函数 -->   
  2.     <c:set var="convert" value="2435"></c:set>   
  3.     <b>自定义函数结果:${czc:covertNumberToChinese(convert)}</b>  
	<!-- 调用自定义函数 -->
    	<c:set var="convert" value="2435"></c:set>
    	<b>自定义函数结果:${czc:covertNumberToChinese(convert)}</b>



         测试结果如下:

 


来自:http://chaozhichen.iteye.com/blog/972455

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值