码迷,mamicode.com
首页 > 其他好文 > 详细

创建自定义的EL function

时间:2015-05-31 22:57:20      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

测试环境: 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")}

 

创建自定义的EL function

标签:

原文地址:http://www.cnblogs.com/david-rui/p/4542860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!