Java --- 自定义EL函数

一、创建UserDefinedELFunction类,用于处理函数业务

 

public class UserDefinedELFunction {
	public static String toUpperCase(String s){
		return s.toUpperCase();
	}
}

 

 

 

二、在WEB-INF下创建userDefinedELFunction.tld文件

<?xml version="1.0" encoding="UTF-8"?>
<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>
	<!-- 短名称:一般和tld文件的文件名保持一致 -->
	<short-name>elf</short-name>
	<!-- 把tld文件绑定到一个名称空间上。只是一个名字,没有实际意义。 -->
	<uri>http://www.xxx.com/jsp/functions</uri>
	<function><!-- 定义函数 -->
		<name>toUpperCase</name><!-- EL函数名称 -->
		<function-class>com.xxx.functions.UserDefinedELFunction</function-class><!-- 方法所在的类全名 -->
		<!-- 方法的签名:类的全名称。只有基本类型不需要 -->
		<function-signature>java.lang.String toUpperCase( java.lang.String )</function-signature>
	</function>
</taglib><!-- 版本号 -->
	<tlib-version>1.0</tlib-version>
	<!-- 短名称:一般和tld文件的文件名保持一致 -->
	<short-name>elf</short-name>
	<!-- 把tld文件绑定到一个名称空间上。只是一个名字,没有实际意义。 -->
	<uri>http://www.xxx.com/jsp/functions</uri>
	<function><!-- 定义函数 -->
		<name>toUpperCase</name><!-- EL函数名称 -->
		<function-class>com.xxx.functions.UserDefinedELFunction</function-class><!-- 方法所在的类全名 -->
		<!-- 方法的签名:类的全名称。只有基本类型不需要 -->
		<function-signature>java.lang.String toUpperCase( java.lang.String )</function-signature>
	</function>
</taglib>

 

 

三、在web.xml中配置taglib,可省略

<?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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 
  <jsp-config>
  	<taglib>
  		<taglib-uri>http://www.xxx.com/jsp/functions</taglib-uri>
  		<taglib-location>/WEB-INF/userDefinedELFunction.tld</taglib-location>
  	</taglib>
  </jsp-config>
   -->
</web-app>
<jsp-config>
  	<taglib>
  		<taglib-uri>http://www.xxx.com/jsp/functions</taglib-uri>
  		<taglib-location>/WEB-INF/userDefinedELFunction.tld</taglib-location>
  	</taglib>
  </jsp-config>
   -->
</web-app>

 

 

四、在JSP中映入taglib

<%@ page import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.xxx.com/jsp/functions" prefix="elf"%>
<html>
  <head>
	<title>自定义EL函数</title>  
  </head>
  
  <body>
    <%
    	pageContext.setAttribute("s", "abcdefg");
     %>
     ${s}<br/>
     ${elf:toUpperCase(s)}<br/>
  </body>
</html><%@ taglib uri="http://www.xxx.com/jsp/functions" prefix="elf"%>
<html>
  <head>
	<title>自定义EL函数</title>  
  </head>
  
  <body>
    <%
    	pageContext.setAttribute("s", "abcdefg");
     %>
     ${s}<br/>
     ${elf:toUpperCase(s)}<br/>
  </body>
</html>

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值