创建自定义的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/:

<?xml version="1.0" encoding="ISO-8859-1" ?>
    <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/javaee/web-jsptaglibrary_2_1.xsd" 
      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.xml below:

 <web-app 
        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-app_2_4.xsd"
        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")}

 

posted @ 2015-05-31 22:32  菟丝子  阅读(298)  评论(0编辑  收藏  举报

============================================================================== 
我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
实我是一个程序员

==============================================================================