EL表达式-自定义函数

表达式语言除了可以使用基本的运算符外,还可以使用自定义函数。通过使用自定义函数,加强了表达式语言的功能。

EL表达式函数,主要功能是完成对数据的修改,统一化格式;

开发步骤

1.开发函数处理类,处理类就是普通的类;每个函数对应类中的一个静态方法;
2. 建立TLD文件,定义表达式函数

3.在WEB.XML文件中配置;(可省略)

4.在JSP页面内导入并且使用

 

案例说明

1.开发函数处理类

package mytag;
/**
 * EL表达式函数处理类
 */
public class ElTag {
	
	public static String reverse(String name){
		return new StringBuffer(name).reverse().toString();
	}
	
	public static int countChar(String text){
		return text.trim().length();
	}
}

  2.创建TLD文件;

<?xml version="1.0" encoding="GBK"?>
<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>el</short-name>
	<!-- 定义第一个函数 -->
	<function>
		<!-- 定义第一个函数:reverse -->
		<name>reverse</name>
		<!-- 定义函数处理类 -->
		<function-class>mytag.ElTag</function-class>
		<!-- 定义函数的对应方法 -->
		<function-signature>
			java.lang.String reverse(java.lang.String)
		</function-signature>
	</function>
	
	<function>
		<name>countChar</name>
		<function-class>mytag.ElTag</function-class>
		<function-signature>
			java.lang.Integer countChar(java.lang.String)
		</function-signature>
	</function>
	
</taglib>

 

3.在WEB中配置

<?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>/eltag</taglib-uri>
			<!-- 配置标签的TLD文件地址 -->
			<taglib-location>/WEB-INF/ElTag.tld</taglib-location>
		</taglib>
	</jsp-config>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

4.JSP页面引入 并且使用

<%@ taglib uri="/eltag" prefix="el" %> 

  <body>
	${el:reverse("ad") }
  </body>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值