自定EL函式

例如:${math: gcd(10,20) }

步骤:

(1)    撰写函式类别

所有函式都是公开且静态的,编译完成后放在WEB-INF\classes\下即可

MathTools.java

package onlyfun.caterpillar; 
public class MathTools { 
    public static int gcd(int m, int n) { 
        int r = 0; 
        while(n != 0) { 
            r = m % n; 
            m = n; 
            n = r; 
        } 
        return m; 
    }  
}

(2)    撰写标签函式描述(Tag Library Descriptor)

Mathtools.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"> 
    <description>MathTools</description> 
   <tlib-version>1.0</tlib-version> 
   <short-name>SimpleMathTools</short-name> 
   <uri>/SimpleMathTools</uri>
    <function> 
        <description>GCDTool</description> 
        <name>gcd</name>
        <function-class>
            onlyfun.caterpillar.MathTools
        </function-class> 
        <function-signature>
            int gcd(int,int)
        </function-signature>    
    </function>
</taglib>

【<function-signature>与<name>对应,<name>是EL呼叫函式所用的名称,而<function-signature>定义了函式的传入参数和返回值。】

(3)    在web.xml中说明class与tld的位置信息

web.xml

【<taglib-url>用来设定使用.tld时的命名空间】

...
    <jsp-config> 
        <taglib> 
            <taglib-uri>
                http://caterpillar.onlyfun.net/
            </taglib-uri> 
            <taglib-location>
                /WEB-INF/tlds/mathtools.tld
           </taglib-location> 
        </taglib> 
    </jsp-config> 
...

(4)    在jsp网页中指定标签函式位置与前置文字

elFunction.jsp

【前置文字的作用是当有许多同名函式时,如两个位置的函式库,而当中有相同的函式时,可以根据前置来识别】

<%@taglib prefix="math"   uri="http://www.caterpillar.onlyfun.net/"%>
<html> 
  <body> 
    Math Tools GCD Test: ${math:gcd(100, 14) }<br>
  </body> 
</html> 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值