你的位置:首页 > Java教程

[Java教程]创建自定义的EL function(Creating a custom EL function)


测试环境: tomcat7,jdk1.7

 

1.First make a class with a static method like so:

package your.package;public class Functions {    public static String hello(String name) {     return "Hiya, " + name + ".";    }}

 

2. Then make a file called mytaglib.tld in WEB-INF/tags/:

<?    ="http://www.w3.org/2001/    xsi:schemaLocation="http://java.sun.com/    version="2.1">    <tlib-version>1.0</tlib-version>   <uri>http://www.your.url/tablib_name</uri>   <function>     <name>hello</name>     <function-class>your.package.Functions</function-class>     <function-signature>java.lang.String hello(java.lang.String)</function-signature>   </function>   </taglib>   

3. The uri would be used if we were accessing this directly, but instead we’ll be accessing the url from the web.

 <web-app     ="http://java.sun.com/    ="http://www.w3.org/2001/    xsi:schemaLocation="http://java.sun.com/    version="2.4">    ...    <jsp-config>    <taglib>     <taglib-uri>      http://some.thing/mine     </taglib-uri>     <taglib-location>      /WEB-INF/tags/mytaglib.tld     </taglib-location>    </taglib>    </jsp-config>    ...   </web-app>

4. Note we’re pointing to the mytaglib.tld file just created. And taglib-uri is how we’ll refer to it in the JSP:

  <%@ taglib uri="http://some.thing/mine" prefix="a" %>   ${a:hello("Aaron")}