欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

EL 表达式中使用自定义函数  

程序员文章站 2022-06-08 09:16:16
...

一、Java

package test.utils.ctag;


public class CustomTags {
	public static boolean equals(String s1,String s2){
		if(s1==s2)
			return true;
		if(s1!=null)
			return s1.equals(s2);
		return false;
	}
}

二、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 web-jsptaglibrary_2_0.xsd"
	version="2.0">
	<tlib-version>1.0</tlib-version>
	<jsp-version>2.0</jsp-version>
	<short-name>f</short-name>
	<function>
		<name>equals</name>
		<function-class>test.utils.ctag.CustomTags</function-class>
		<function-signature>boolean equals( java.lang.String,
			java.lang.String )</function-signature>
	</function>
</taglib>


三、web.xml

  <jsp-config>
    <taglib>
        <taglib-uri>/functions</taglib-uri>
        <taglib-location>/WEB-INF/classes/functions.tld</taglib-location>
    </taglib>
  </jsp-config>

四、Jsp

<%@ taglib prefix="f" uri="/functions" %>
${f:equals("Hello","Hello")}<br/>
 ${f:equals("Hello","World")}<br/>
 ${f:equals(null,"World")}