如果已有的el函数不能满足项目要求,可以开发出自己定义的EL函数。
1 写一个普通的java类,该类必须是public,且java方法必须是静态方法
package com.tag;
public class CustomFun {
public static boolean matches(String s){
return s.equals("wangli");
}
}
2 在TLD文件中配置EL自定义函数
<?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/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<uri>http://shun.com/customfun</uri>
<function>
<name>matches</name>
<function-class>com.tag.CustomFun</function-class>
<function-signature>java.lang.Boolean matches(java.lang.String)</function-signature>
</function>
</taglib>
<uri> 必须是唯一滴。<name>指定使用EL自定义函数时的名称 ${customfun:matches}
3 页面使用el自定义函数
<%@ taglib uri="http://shun.com/customfun" prefix="customfun"%>
${customfun:matches("wangli") }
会看到输出为true
或者 在web.xml中添加
<jsp-config>
<taglib>
<taglib-uri>/tags/customfun</taglib-uri>
<taglib-location>/WEB-INF/tld/customfun.tld</taglib-location>
</taglib>
</jsp-config>
修改jsp页面
<%@ taglib uri="/tags/customfun" prefix="customfun"%>
${customfun:matches("wangli") }