EL表达式--自定义标签库

EL(Expression Language:

目的:为了使JSP写起来更加简单。表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 JSP 中简化表达式的方法。

 

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

开发步骤

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

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

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

 

案例说明

1.开发函数处理类

Java代码 复制代码 收藏代码
  1. package mytag;
  2. /**
  3. * EL表达式函数处理类
  4. */
  5. publicclass ElTag {
  6. publicstatic String reverse(String name){
  7. returnnew StringBuffer(name).reverse().toString();
  8. }
  9. publicstaticint countChar(String text){
  10. return text.trim().length();
  11. }
  12. }
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文件;

 

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="GBK"?>
  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
  5. http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  6. version="2.0">
  7. <!-- 定义函数版本 -->
  8. <tlib-version>1.0</tlib-version>
  9. <!-- 定义函数名称 -->
  10. <short-name>el</short-name>
  11. <!-- 定义第一个函数 -->
  12. <function>
  13. <!-- 定义第一个函数:reverse -->
  14. <name>reverse</name>
  15. <!-- 定义函数处理类 -->
  16. <function-class>mytag.ElTag</function-class>
  17. <!-- 定义函数的对应方法 -->
  18. <function-signature>
  19. java.lang.String reverse(java.lang.String)
  20. </function-signature>
  21. </function>
  22. <function>
  23. <name>countChar</name>
  24. <function-class>mytag.ElTag</function-class>
  25. <function-signature>
  26. java.lang.Integer countChar(java.lang.String)
  27. </function-signature>
  28. </function>
  29. </taglib>
<?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中配置

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6. <jsp-config>
  7. <taglib>
  8. <!-- 配置标签的引用地址 JSP页面中引用时使用-->
  9. <taglib-uri>/eltag</taglib-uri>
  10. <!-- 配置标签的TLD文件地址 -->
  11. <taglib-location>/WEB-INF/ElTag.tld</taglib-location>
  12. </taglib>
  13. </jsp-config>
  14. <welcome-file-list>
  15. <welcome-file>index.jsp</welcome-file>
  16. </welcome-file-list>
  17. </web-app>
<?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页面引入 并且使用

Java代码 复制代码 收藏代码
  1. <%@ taglib uri="/eltag" prefix="el" %>
  2. <body>
  3. ${el:reverse("ad") }
  4. </body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值